質問

How do I compare the String

http://192.168.74.1/sp/info.php?prodnum=0000000001

with

http://192.168.74.1

I only need to get the http://192.168.74.1 part of first string.

役に立ちましたか?

解決

You don't need to extract anything from the first string. You can use String.startsWith:

if (string1.startsWith(string2)) {
  // do something
}

他のヒント

1) you can use beginwith function

String Mainstring= "http: // 192.168.74.1/sp/info.php?prodnum=0000000001";
if (Mainstring.startsWith("http: // 192.168.74.1"))
 {
 //Write your code here
 }

2) you can use contains function

boolean retval = Mainstring.contains(cs1);

retval will be true if it exist otherwise false.

you can do

let

 str = "http://192.168.74.1/sp/info.php?prodnum=0000000001" 
then
if(str.contains("http://192.168.74.1/"))
{
//do what you want
}

If you need to check if the String Str starts with another string try:

Str= "http: // 192.168.74.1/sp/info.php?prodnum=0000000001";

if (Str.startsWith("http: // 192.168.74.1"))
 {
 //do something here
 }

Consider you have String1 and String2.

To check if String1 starts with String2 , simply use String1.startsWith(String2) method

Here is the example:

String str = "abcd";
String newstr = "ab";

boolean flag = str.startsWith(newstr); // returns true

Please comment for further help.

use

if(aString.contains("http://192.168.74.1")){
 //do something here
}

Although checking .startsWith(...) is sufficient, since you want to match urls, it is better to parse the url and break it into components. This can be done manually, using regular expressions, but it is probably safer to use the java url parser. See parsing a URL for details.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top