Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extracting string using IndexOf ? 1

Status
Not open for further replies.

Ngai88

Programmer
Sep 8, 2004
56
US
Hello,
I want to extract the first name off a list of names separated by '<br>', what is the correct way of doing it?

i.e.

John<br>James<br>Lisa<br>Mary<br>

I want the name 'John' only.

thanks,
Ngai
 
Code:
String line = "John<br>James<br>Lisa<br>Mary<br>";
String john = line.substring(0, line.indexOf("<br>"));
Or to break the String into an array of Strings holding te names :

Code:
		String line = "John<br>James<br>Lisa<br>Mary<br>";
		String[] names = line.split("<br>");
		for (int i = 0; i < names.length; i++) {
			System.out.println(names[i]);
		}

--------------------------------------------------
Free Database Connection Pooling Software
 
If you want to get only the first one, I'd use the first sedj's example. But be sure there is at least one "<br>" or an exception will be thrown.

If you want to take all names, use the second one. But the method split(), AFAIK is since JDK 1.4. If you have an older version you can use StringTokenizer class.

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top