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.

Recommended Answers

All 4 Replies

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.

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 {
}

Nice, thanks alot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.