Extracting a value from a String

I am getting back a String from a function in this format:

Underlying><Code>KGF</Code></Underlying>

Please note that a string is being returned and not an XML.

I need to extract KGF or any other value from this.

The String may contain various <Code> like ABCDS or HSBC or BloomBerg.

Hence I cannot use substring because I cannot use substring(codeReturned,20,<Dont know last element>)

DO I need to use index of? Can anyone please guide?

Please note that a string is being returned and not an XML.

[578 byte] By [bhuru_luthriaa] at [2007-11-27 2:37:23]
# 1
> Please note that a string is being returned and not an XML.Regex to remove everything between < and >.
mlka at 2007-7-12 2:57:32 > top of Java-index,Java Essentials,Java Programming...
# 2
Sorry, i dont understand what u have said said
bhuru_luthriaa at 2007-7-12 2:57:32 > top of Java-index,Java Essentials,Java Programming...
# 3

Please make the extra effort to write out words such as "you" . The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership.

Strip everything between the < and the >. Either in a loop (pusdo code):

remove = false;

for i = 0 to string.length

char = string.charAt( i )

if char == '<' remove = true;

if remove then string.remove( i );

if char == '>' remove = false

If you like you can also do this with something called a [url=http://en.wikipedia.org/wiki/Regular_expression]Regular Expression[/url].

mlka at 2007-7-12 2:57:32 > top of Java-index,Java Essentials,Java Programming...
# 4

Just because you are not getting an XML file, does not mean you are not getting XML. If that is a literal example of one of the Strings you are getting, then you are getting a String that contains XML, so you can parse it as if it were an XML file. Simply

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document dom = db.parse(new ByteArrayInputStream(string.getBytes()));

And you have loaded the String into an XML Parser.

Of course, the other Option is regex.

A quick hack:

String string1 = string.replaceAll("^.*<Code>([^<]*)</Code>.*$", "\1");

masijade.a at 2007-7-12 2:57:32 > top of Java-index,Java Essentials,Java Programming...
# 5
Hi You have not specified it wheather <tag> occurs only once or it can ocur multiple times.If it occurs one time only then you can use indexOf() method of the String class easily.Thanks & RegardsPradeep
kumarpraa at 2007-7-12 2:57:32 > top of Java-index,Java Essentials,Java Programming...
# 6

if your Strings appear always like that you may use this code perhaps

String ss = "<Underlying><Code>KGF</Code></Underlying>";

int ret = 0;

String mean = "";

String kgf ="";

for(int i=0;i<ss.length()-1;i++)

{

if(ss.substring(i, i+1).equals(">") && !ss.substring(i, i+2).equals("><"))

{

ret = i;

}

}

mean = ss.substring(ret+1,ss.length());

System.out.println(mean);

f:

for(int k=0;k<mean.length();k++)

{

if(!mean.substring(k, k+1).equals("><"))

{

kgf=kgf+mean.substring(k, k+1);

}

else

{

break f;

}

}

kgf = "KGF"

ayberk cansever

ayberka at 2007-7-12 2:57:32 > top of Java-index,Java Essentials,Java Programming...