简体   繁体   English

java:在字符串中找到一个单词,如果在特定位置用新单词替换

[英]java : Find a word in a string and replace it with a new word if its at a specific position

I have a sample string such as : 我有一个示例字符串,例如:

" I Love cricket. I Love football. I Love tennis. I Love every game!!! " “我喜欢板球。我喜欢足球。我喜欢网球。我喜欢每场比赛!!”

Now what I want to do is first time replace the the I (at position 1 ) to " We ". 现在,我要做的是第一次将I (位置1 )替换为“ We ”。 So it becomes 所以变成

" We Love cricket. I Love football. I Love tennis. I Love every game!!! " 我们喜欢板球。我喜欢足球。我喜欢网球。我喜欢每场比赛!!!”

Again in the second run I want to replace I (at position 7 ) to " We " . 再次在第二轮中,我想将I (在位置7 )替换为“ We ”。

" I Love cricket. I Love football. We Love tennis. I Love every game!!! " “我喜欢板球。我喜欢足球。 我们喜欢网球。我喜欢每场比赛!!!”

So what I mean to say is replacing a word at a specific position with a new word. 所以我的意思是用新单词替换特定位置的单词。 I tried following things so far: 到目前为止,我尝试了以下操作:

  1. ReplaceAll - worked for only when the words are uniques but fails in every other scenario. ReplaceAll仅在单词是唯一的但在其他所有情况下都失败时才起作用。
  2. StringTokenizer - used it go get an arraylist. StringTokenizer用它去获取一个数组列表。 replaced the word at the spcific position and then again appended all elements in the arraylist. 在特定位置替换单词,然后再次在arraylist中附加所有元素。 Issue is the special characters are lost. 问题是特殊字符丢失。

Please suggest a better way out of doing it. 请提出一种更好的解决方案。

EDIT: The delimeter can change and isn't specifically a " ". 编辑:分隔符可以更改,并且不是专门的“”。

yourString.split(" ") gives an array of words. yourString.split(" ")给出一个单词数组。 Assign new value to the specified index and then append the entries via space(" ") delimiter to produce new resulting string. 将新值分配给指定的索引,然后通过space(“”)分隔符附加条目以产生新的结果字符串。

public static void main(String args[]) throws Exception 
{
    String testString = " I Love cricket. I Love football. I Love tennis. I Love every game!!! ";
    String[] words = testString.split(" ");
    words[7] = "We";
    String newString = "";

    for (int i = 0; i < words.length; i++)
        newString += " " + words[i];

    System.out.println(newString);
}
StringBuilder sb = 
    new StringBuilder(" I Love cricket. I Love football. I Love tennis. I Love every game!!! ")

sb.replace(1, 2, "We")
  .replace(35, 36, "We")

return sb.toString()

If you need to dynamically find positions of "I" and replace some of them with "We" then it's another story, but you get the idea. 如果您需要动态查找“ I”的位置并将其中一些位置替换为“ We”,那么这是另一回事了,但是您知道了。

replace(int start, int end, String str): Replaces the characters in a substring of this sequence with characters in the specified String. replace(int start,int end,String str):用指定String中的字符替换此序列的子字符串中的字符。 It returns this so you can chain several replace calls. 它返回此值,因此您可以链接多个替换调用。

改用StringBuilder ,使用insert()方法。

Instead of using replaceAll use straight regex with Pattern . 与其使用replaceAll使用Pattern的正则表达式。 And just replace your first match rather than global. 并替换您的第一个比赛而不是全局比赛。 Or just use replaceFirst() 或者只是使用replaceFirst()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM