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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Loop with indexOf ?

Status
Not open for further replies.

jumper2

Programmer
Jul 13, 2007
41
ES
Hello,
I have a problem to write an algoritm which goes thrue on my String which looks like :

String myText = "<table><tr><td><input type="Text" name="address1" value="test1@test.com"></td></tr>
<tr><td><input type="Text" name="address2" value="test2@test.com"></td></tr>
<tr><td><input type="Text" name="address3" value="test3@test.com"></td></tr>
</table>";


I would like to get back with a loop all 'place' where mysearch_start="value=\""; and mysearch_finish=".com\">";

I tried like: int mysearch_start = myText.indexOf("value=\"");

int mysearch_end =
myText.indexOf(".com\">");

but I don't get back the all 3 what I would need. Is anybody has idea how could I do it?
 
The indexOf method simply returns the first match it finds. To get all three, you'd need to record the position of the first match, and use it (incremented by the number of characters in the string matched for, in this case 5) as the start point for the next search, and repeat until you get no more matches. Use the indexOf(String, int) method for this, where the second parameter is the start point of the search.

Tim
 
I'd consider the use of String.split in this case.

Anyway, here you have an example for the loop: Java Strings

Cheers,
Dian
 
Use the class java.util.Scanner:

Code:
 public ScannerFoo (String param)
	{
		Scanner scanner = new Scanner (param);
		scanner.useDelimiter ( "( value=.)|(.com\">)");
		while (scanner.hasNext ()) 
		{
			String s = scanner.next ();
			if (scanner.hasNext ())
			{
				s = scanner.next ();
				System.out.println (s);
			}
		}
	}

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top