0

I need some help with editing a string and adding parts of that String to an ArrayList of strings:

public ArrayList<String> strings = new ArrayList<String>();

Basically my program reads a file from the internet and sets the contents of the read file as a String. I then want to read through the strings contents to find keywords like:

website=http://google.com
website=http://stackoverflow.com

I just want to add the parts after the website= to the array of strings, I also want to take into account that the website part is there because I will also have other things apart from website= such as ping=. I just don't know how I would do this.

If anybody could help me with this or explain a better way I could go about doing the hole method itself, that would help me out a lot.

4

2 回答 2

2

So you have all that in one string? Then regular expressions might be a way to go, e.g. Pattern and Matcher along with the expression website=(\S+).

Example:

String input = "website=http://google.com ping=http://pong.com website=http://stackoverflow.com";

Pattern p = Pattern.compile( "website=(\\S+)" );
Matcher m = p.matcher( input );

while( m.find() )
{
  System.out.println(m.group(1));
}

Output:

http://google.com
http://stackoverflow.com
于 2013-10-17T11:42:15.260 回答
0

There is no single way but some things you can use are : 1. String.contains() Function : Use this to see if a string contains a substring. To account for case sensivity , you can use like :

str = "website="
yourString.toLowerCase().contains(str.toLowerCase())

You can combine with if logic like :

if(yourString.toLowerCase().contains(str.toLowerCase())
//do something

You can get index of the character following website= like :

str="website=";
int index = yourString.indexOf(str)+str.length();

Use all the above to write a logic for your needs.

于 2013-10-17T11:47:58.547 回答