Hey everyone, i need a little help with searching a string and putting that date into another string.
ex:

String com = "";
String mystring = "sometext here: !com shutdown";

// Magic happens

System.out.println(com);

// Output: "shutdown"

"!com" is the constant to search for, shutdown is variable.

Hey everyone, i need a little help with searching a string and putting that date into another string.
ex:

String com = "";
String mystring = "sometext here: !com shutdown";

// Magic happens

System.out.println(com);

// Output: "shutdown"

"!com" is the constant to search for, shutdown is variable.

maybe this is what you want:

String identifier="!com";

com =mystring.substring(mystring.indexOf(identifier)+identifier.length());

as you can see we break the string up using substring(), we then find the index of the identifier '!com" and we add to it its own length this is so that our output wont show "!com shutdown". you might also want to do a trim() on your final output so no white spaces are shown.

Edited 4 Years Ago by DavidKroukamp: n/a

Thanks a lot!
While i have you, is there any way i can check if the String contains the identifier before i do the substring() ?

About the trim() can't i just make the identifier = "!com " instead of "!com" ?

Thanks a lot!
While i have you, is there any way i can check if the String contains the identifier before i do the substring() ?

About the trim() can't i just make the identifier = "!com " instead of "!com" ?

as for the trim yes you can do that intsead....

and you can just use the contains() method like so:

if(mystring.contains("!com")) {
} else {
}
This question has already been answered. Start a new discussion instead.