78

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...

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
Waypoint
  • 17,283
  • 39
  • 116
  • 170

6 Answers6

200

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.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
2

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.

Flores
  • 31
  • 3
2

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/
AllanRibas
  • 678
  • 5
  • 14
1
String str = "to";
str.replace("to", "xyz");

Just try it :)

FBRNugroho
  • 710
  • 6
  • 8
-2

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

Empire of E
  • 588
  • 6
  • 12
-3

You're doing only one mistake.

use replaceAll() function over there.

e.g.

String str = "Hi";
String str1 = "hello";
str.replaceAll( str, str1 );
Mike G
  • 4,232
  • 9
  • 40
  • 66
krupa parekh
  • 500
  • 3
  • 7
  • 8
    -- Neither the `replaceAll() ` function changes the original string, it returns the processed string as a result! – rekaszeru Sep 27 '11 at 08:43