Reputation: 1929
I have to remove a particular token from a String variable.
for eg:
If the String variable is like "GUID+456709876790"
I need to remove the "GUID+"
part from the string and only need "456709876790"
.
How can it be done?
Upvotes: 2
Views: 8657
Reputation: 1
this works for me
String Line="test line 1234 abdc",aux;
token=new StringTokenizer(Line);
while(token.hasMoreTokens())
if(!("1234").equals(aux=token.nextToken())){
new_line+= aux+" ";
System.out.println("la nueva string es: "+ new_line);
}
Upvotes: 0
Reputation: 12531
If you're using apache.commons.lang
library you can use StringUtils
just do:
StringUtils.remove(yourString, token);
Upvotes: 2
Reputation: 15644
Just try this one :
String a = "GUID+456709876790";
String s = a.replaceAll("\\D","");
I am assuming that you want only digits as I have used regex here to remove any thing that is not a digit
Upvotes: 1
Reputation: 6572
String str = "GUID+456709876790"
str.substring(str.indexOf("+")+1)
Upvotes: 1
Reputation: 117587
String s = "GUID+456709876790";
String token = "GUID+";
s = s.substring(s.indexOf(token) + token.length());
// or s = s.replace(token, "");
Upvotes: 3
Reputation: 1500495
Two options:
As you're just removing from the start, you can really easily use substring:
text = text.substring(5);
// Or possibly more clearly...
text = text.substring("GUID+".length());
To remove it everywhere in the string, just use replace:
text = text.replace("GUID+", "");
Note the use of String.replace()
in the latter case, rather than String.replaceAll()
- the latter uses regular expressions, which would affect the meaning of +
.
Upvotes: 7