Android - 如何用另一个字符串替换部分字符串?

时间:2020-12-31 07:10:46

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?

我有一些带有一些数字和英文单词的字符串,我需要将它们翻译成我的母语,找到它们并用这个单词的locallized版本替换它们。你知道如何轻松地替换字符串中的单词吗?

Thanks

谢谢

Edit:

编辑:

I have tried (part of a string "to" should be replaced by "xyz"):

我试过(字符串“to”的一部分应该用“xyz”代替):

string.replace("to", "xyz")

But it is not working...

但它不起作用......

5 个解决方案

#1


164  

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变量,或者它自己:

string = string.replace("to", "xyz");

or

要么

String newString = string.replace("to", "xyz");

API Docs

API文档

public String replace (CharSequence target, CharSequence replacement) 

Since: API Level 1

自:API级别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.

返回结果字符串。如果target或replacement为null,则抛出NullPointerException。

#2


1  

String str = "to";
str.replace("to", "xyz");

Just try it :)

去尝试一下 :)

#3


1  

MAY BE INTERESTING TO YOU:

可能对你有兴趣:

In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.

在java中,字符串对象是不可变的。永恒只是意味着不可修改或不可改变。

Once string object is created its data or state can't be changed but a new string object is created.

创建字符串对象后,无法更改其数据或状态,但会创建新的字符串对象。

#4


-2  

You're doing only one mistake.

你只犯了一个错误。

use replaceAll() function over there.

在那里使用replaceAll()函数。

e.g.

例如

String str = "Hi";
String str1 = "hello";
str.replaceAll( str, str1 );

#5


-2  

rekaszeru

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

我注意到你在2011年发表评论,但我认为无论如何我应该发布这个答案,万一有人需要“替换原来的字符串”并遇到这个答案..

Im using a EditText as an example

我使用EditText作为示例


// GIVE TARGET TEXT BOX A NAME

//给目标文本框一个名字

 EditText textbox = (EditText) findViewById(R.id.your_textboxID);

// STRING TO REPLACE

// STRING替换

 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中的新字符串

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() !

这是未经测试的,但它只是使用返回的字符串用setText()替换原始布局字符串的示例!

Obviously this example requires that you have a EditText with the ID set to your_textboxID

显然,此示例要求您具有ID为your_textboxID的EditText

#1


164  

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变量,或者它自己:

string = string.replace("to", "xyz");

or

要么

String newString = string.replace("to", "xyz");

API Docs

API文档

public String replace (CharSequence target, CharSequence replacement) 

Since: API Level 1

自:API级别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.

返回结果字符串。如果target或replacement为null,则抛出NullPointerException。

#2


1  

String str = "to";
str.replace("to", "xyz");

Just try it :)

去尝试一下 :)

#3


1  

MAY BE INTERESTING TO YOU:

可能对你有兴趣:

In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.

在java中,字符串对象是不可变的。永恒只是意味着不可修改或不可改变。

Once string object is created its data or state can't be changed but a new string object is created.

创建字符串对象后,无法更改其数据或状态,但会创建新的字符串对象。

#4


-2  

You're doing only one mistake.

你只犯了一个错误。

use replaceAll() function over there.

在那里使用replaceAll()函数。

e.g.

例如

String str = "Hi";
String str1 = "hello";
str.replaceAll( str, str1 );

#5


-2  

rekaszeru

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

我注意到你在2011年发表评论,但我认为无论如何我应该发布这个答案,万一有人需要“替换原来的字符串”并遇到这个答案..

Im using a EditText as an example

我使用EditText作为示例


// GIVE TARGET TEXT BOX A NAME

//给目标文本框一个名字

 EditText textbox = (EditText) findViewById(R.id.your_textboxID);

// STRING TO REPLACE

// STRING替换

 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中的新字符串

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() !

这是未经测试的,但它只是使用返回的字符串用setText()替换原始布局字符串的示例!

Obviously this example requires that you have a EditText with the ID set to your_textboxID

显然,此示例要求您具有ID为your_textboxID的EditText