I'm trying to do something seemingly simple. I have meta-data with this format
-key=value
I've already split the string at the = but I need to take the - off. I'm trying to use this function key.replaceFirst("-", ""); but it doesn't do anything to the string.
I've tried putting \\ in the regex but that solved nothing.
Solution:
I did not say key = key.replaceFirst("-", "");
You need to assign back return value of replaceFirst as String is immutable object:
key = key.replaceFirst("-", "");
String.replaceFirst does not replace the string in-place, but returns a replaced string.
You need to assign back the return value:
key = key.replaceFirst("-", "");
You are not assigning the string back to it !
key = key.replaceFirst("-", "");
System.out.println(key);
HTH,
Keshava.
Related
I've the following string: {"array 1":[ ....
And I would like to remove everything preceding [.
For that I use: .replace("{\"array 1\":", ""); and that works well.
However, I've several arrays, so I'd like to do the replace based on a variable that holds the array name.
For example:
String arr_name = "array 1";
....replace('{\"arr_name\":", "");
Is it possible to use variable key to replace a string?
EDIT:
I've ended up adding another element to parse the array in JSON which removed its name.
Thank you all for the quick comments and suggestions.
you can do a string format, here is an example of that :
String arr_name = "array 1";
....replace(String.format ("{\"%s\"", arr_name), "");
Just use a substring, starting at the index of [.
String input = "{\"array 1\":[key:value...";
String result = input.substring(input.indexOf('['));
This gives [key.value...
Hello all I an new to java I just want to know that can we convert "Hello" in Hello. I have gone through the internet answers but found that if any string has "" in that so we can use the replace method of java. So I just want to convert the "Hello" into Hello. So if you know please help
suppose
String s="Hello"
//Required Operation
System.out.println(s);//It should print Hello.
So if you know please help me. Actually I have a file which contains lots of data having " " and I only want that data without double quotes so is it possible to convert that.
Here is an example:
String s = "\"hello\"";
String result = s.replaceAll("\"", "");
System.out.println(result);
Actually if you declare your string String s="Hello", the variable s will not contain any quotes, because the quotes are Java syntax and mark the start and end of the String.
Use String.replaceAll()
str = str.replaceAll("\"", "");
As all the other answers you're able to use:
str = str.replaceAll("\"", "");
But their is another solution if you just want to erase the 1st and last char of your string ( so your " here) is to use substring like:
str = "Hello";
str = str.substring(0,str.length()-2);
I think that it could work for you
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?
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 +.
String s = "GUID+456709876790";
String token = "GUID+";
s = s.substring(s.indexOf(token) + token.length());
// or s = s.replace(token, "");
If you're using apache.commons.lang library you can use StringUtils just do:
StringUtils.remove(yourString, token);
String str = "GUID+456709876790"
str.substring(str.indexOf("+")+1)
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
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);
}
I have strings with some numbers and english words and I need to translate them to my mother tongue by finding them and replacing them by locallized version of this word. Do you know how to easily achieve replacing words in a string?
Thanks
Edit:
I have tried (part of a string "to" should be replaced by "xyz"):
string.replace("to", "xyz")
But it is not working...
It is working, but it wont modify the caller object, but returning a new String.
So you just need to assign it to a new String variable, or to itself:
string = string.replace("to", "xyz");
or
String newString = string.replace("to", "xyz");
API Docs
public String replace (CharSequence target, CharSequence replacement)
Since: API Level 1
Copies this string replacing
occurrences of the specified target
sequence with another sequence. The
string is processed from the beginning
to the end.
Parameters
target the sequence to replace.
replacement the replacement
sequence.
Returns the resulting string.
Throws NullPointerException if target or replacement is null.
MAY BE INTERESTING TO YOU:
In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.
Once string object is created its data or state can't be changed but a new string object is created.
In kotlin there is no replaceAll, so I created this loop to replace repeated values in a string or any variable.
var someValue = "https://www.google.com.br/"
while (someValue.contains(".")) {
someValue = someValue.replace(".", "")
}
Log.d("newValue :", someValue)
// in that case the stitches have been removed
//https://wwwgooglecombr/
String str = "to";
str.replace("to", "xyz");
Just try it :)
rekaszeru
I noticed that you commented in 2011 but i thought i should post this answer anyway, in case anyone needs to "replace the original string" and runs into this answer ..
Im using a EditText as an example
// GIVE TARGET TEXT BOX A NAME
EditText textbox = (EditText) findViewById(R.id.your_textboxID);
// STRING TO REPLACE
String oldText = "hello"
String newText = "Hi";
String textBoxText = textbox.getText().toString();
// REPLACE STRINGS WITH RETURNED STRINGS
String returnedString = textBoxText.replace( oldText, newText );
// USE RETURNED STRINGS TO REPLACE NEW STRING INSIDE TEXTBOX
textbox.setText(returnedString);
This is untested, but it's just an example of using the returned string to replace the original layouts string with setText() !
Obviously this example requires that you have a EditText with the ID set to your_textboxID
You're doing only one mistake.
use replaceAll() function over there.
e.g.
String str = "Hi";
String str1 = "hello";
str.replaceAll( str, str1 );
I tried to replace characters in String which works sometimes and does not work most of the time.
I tried the following:
String t = "[javatag]";
String t1 = t;
String t2 = t;
t.replace("\u005B", "");
t.replace("\u005D", "");
t1.replace("[", "");
t1.replace("]", "");
t2.replace("\\]", "");
t2.replace("\\[", "");
System.out.println(t+" , "+t1+" , "+t2);
The resulting output is still "[javatag] , [javatag] , [javatag]" without the "[" and "]" being replaced.
What should I do to replace those "[" and "]" characters ?
String objects in java are immutable. You can't change them.
You need:
t2 = t2.replace("\\]", "");
replace() returns a new String object.
Edit: Because ... I'm breaking away from the pack
And since this is the case, the argument is actually a regex, and you want to get rid of both brackets, you can use replaceAll() instead of two operations:
t2 = t2.replaceAll("[\\[\\]]", "");
This would get rid of both opening and closing brackets in one fell swoop.
Strings are immutable so
t.replace(....);
does nothing
you need to assign the output to some variable like
t = t.replace(....);
Strings in Java are immutable, meaning you can't change them. Instead, do t1 = t1.replace("]", "");. This will assign the result of replace to t1.
String.replace doesn't work that way. You have to use something like t = t.replace("t", "")
String.replace() returns a new string after replacing the required characters. Hence you need to do it in this way:
String t = "[javatag]";
t = t.replace("[","");
t = t.replace("]","");
t.replace(....);
gives you a String (return a string)
you can reassign the origin variable name to the new string
and the old string will later been garbage-collected
:)