Android的 - 如何替换字符串由另一个字符串的一部分? [英] Android - how to replace part of a string by another string?

查看:2753
本文介绍了Android的 - 如何替换字符串由另一个字符串的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数字和英文单词串,我需要寻找他们,并通过这个词locallized版本替换他们能够把它们翻译成我的母语。你知道如何轻松实现在一个字符串替换词?

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?

感谢

编辑:

我已经试过(串到应以某某代替部分):

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

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

但它不工作...

But it is not working...

推荐答案

这是工作,但它不会修改来电的对象,而是返回一个新的String。
所以,你只需将其分配到一个新的字符串变量,或者本身:

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");

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

API文档

public String replace (CharSequence target, CharSequence replacement) 

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

参数

  • 目标的顺序来替换。
  • 替换替换 序列。
  • target the sequence to replace.
  • replacement the replacement sequence.

返回生成的字符串。
抛出 NullPointerException异常如果目标或替换为空。

Returns the resulting string.
Throws NullPointerException if target or replacement is null.

这篇关于Android的 - 如何替换字符串由另一个字符串的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆