Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中替换字符串中的单词_Java_Android - Fatal编程技术网

Java 如何在android中替换字符串中的单词

Java 如何在android中替换字符串中的单词,java,android,Java,Android,我有一个字符串,值为hi,how r u{name}; 我还有一个大小为3的数组 我想从字符串中找到单词“{name}”,并用当前数组值a[I]替换它。 我已经尝试了这个代码 Resources res = getResources (); String []Questions=res. getStringArray (R.array.faq_ques); int Questions_Array_length = Questions .length; for(i=0; i<Questi

我有一个字符串,值为hi,how r u{name}; 我还有一个大小为3的数组 我想从字符串中找到单词“{name}”,并用当前数组值a[I]替换它。 我已经尝试了这个代码

Resources res = getResources ();
String []Questions=res. getStringArray (R.array.faq_ques);

int Questions_Array_length = Questions .length;

for(i=0; i<Questions_Array_length; i++) {
  String Amith= "hi how r u {Name}";
  Amith.replace("{Name}", Questions[i]);
}
Resources res=getResources();
String[]Questions=res.getStringArray(R.array.faq_ques);
int Questions\u Array\u length=Questions.length;
对于(i=0;i按如下方式操作:

Amith = Amith.replace("{Name}", Questions[i]);
在API级别1中添加

复制此字符串以替换指定目标的出现 序列与另一个序列。该字符串是从 从头到尾。参数以要替换的序列为目标。 替换替换序列。返回

the resulting string.
如果目标或替换为null,则引发NullPointerException


由于问题不清楚,我假设这部分不起作用。
Amith.replace({Name}),Questions[I]);
意味着
字符串Amith
没有被替换。您需要执行
Amith=Amith.replace({Name}),Questions[I]);
因为字符串是不可变的,字符串的结果
是具有替换值的新字符串

Resources res = getResources ();
String []Questions=res. getStringArray (R.array.faq_ques);

int Questions_Array_length = Questions .length;
for(i=0;i<Questions_Array_length;i++)
{
  String    Amith= "hi how r u {Name}";
  Amith=Amith.replace("{Name}", Questions[i]);
}
Resources res=getResources();
String[]Questions=res.getStringArray(R.array.faq_ques);
int Questions\u Array\u length=Questions.length;

对于(i=0;i某些东西,这就是你所需要的

        String sentence = "hello what is your name";
        String[] words = sentence.split(" ");
        for(int i = 0; i<words.length; i++){
            if(words[i].equals("Your String")){
                words[i] = "Your New String";
            }
        }

        String newSentence = "";
        for(int i = 0; i<words.length; i++){
            newSentence += words[i];
        }
String句子=“你好,你叫什么名字”;
字符串[]单词=句子。拆分(“”);
对于(int i=0;i
        String sentence = "hello what is your name";
        String[] words = sentence.split(" ");
        for(int i = 0; i<words.length; i++){
            if(words[i].equals("Your String")){
                words[i] = "Your New String";
            }
        }

        String newSentence = "";
        for(int i = 0; i<words.length; i++){
            newSentence += words[i];
        }