2

I want to get the particular part of String from a complete word.

For example:

I have the following String.

# STRING_VALUES #

and a sentence is as follows

<p># STRING_VALUE #<br /># CMESSAGE #<br /># CUSTOMERADDRESS #<br /><br /></p>

From the above, I want to get the string as follows:

STRING_VALUES
CMESSAGE
CUSTOMERADDRESS

How to do to get the above string? I am stuck with this.

Babu R
  • 1,025
  • 8
  • 20
  • 40
  • 3
    What have you tried? Have you at least read [the javadoc of the class String](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html)? – JB Nizet Sep 25 '12 at 11:02
  • 1
    Are they all surrounded by a pair of # symbols? What have you tried? –  Sep 25 '12 at 11:02
  • 1
    String functions are available in java. chk it.. http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html – Sin Sep 25 '12 at 11:03
  • "particular part of String"? Can you describe that particular part please? In that case, people will be able to help you easily. – Juvanis Sep 25 '12 at 11:03
  • particular part is without #. ie. STRING_VALUES – Babu R Sep 25 '12 at 11:37

7 Answers7

9

Use split:

String ss ="# STRING_VALUES #";
String[] parts = ss.split("#");
System.out.println(parts[1].trim()); // STRING_VALUES

DEMO.

João Silva
  • 89,303
  • 29
  • 152
  • 158
3

For your specific case following code will work:

String str = "# STRING_VALUES #";
String result = str.substring(2,str.length()-2);

UPDATE: (Following code will work for you modified input string)

String str = "<p># STRING_VALUE #<br /># CMESSAGE #<br /># CUSTOMERADDRESS #<br /><br /></p>";

String[] tokens = str.substring(3, str.length()-4).split("<br />");
// tokens will contain [0] = "# STRING_VALUE #"
//                     [1] = "# CMESSAGE #"
//                     [2] = "# CUSTOMERADDRESS #"
//                     [3] = ""

ArrayList<String> results = new ArrayList<String>();

for(String  token : tokens)
{
    if(token.length() != 0)
    {
        results.add(token.substring(2,str.length()-2));
    }
}

// results has required strings
for(String  result : results)
{
    System.out.println(result);
}
Azodious
  • 13,752
  • 1
  • 36
  • 71
0

You could do that with regular expressions. See the classes Pattern and Matcher. The regular expression to find the STRING_VALUE string in your example would be:

#\s(\w*)\s#

This would return a chain of word-characters (letters, numbers and underscores) after "#" and a whitespace and before a whitespace and "#". The chain of word characters would be returned as a sub-match you can extract separately.

Alternatively, you could use String.indexOf to search for the positions of delimeters in your string and String.substring to extract parts between the delimeter position you found.

Philipp
  • 67,764
  • 9
  • 118
  • 153
0

Look at http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

They will tell you:

Here are some more examples of how strings can be used:

     System.out.println("abc");
     String cde = "cde";
     System.out.println("abc" + cde);
     String c = "abc".substring(2,3);
     String d = cde.substring(1, 2);
Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Jos
  • 1,015
  • 2
  • 13
  • 25
0

You use the String[] split(String regex) method of the String class as documented here.

coredump
  • 3,017
  • 6
  • 35
  • 53
0

If your string would be of this format i.e., space seperated you could use String.split()

String ss ="# STRING_VALUES #";
    System.out.println(ss.split("\\s")[1]);

the above code return STRING_VALUES as output

PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

You could use substring:

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#substring(int, int)

Or you could use regex: Need regexp to find substring between two tokens

Community
  • 1
  • 1
Víctor Herraiz
  • 1,132
  • 1
  • 11
  • 26