在字符串中如何给特定单词出现的位置添加前缀和后缀?

huangapple go评论90阅读模式
英文:

How to add a prefix and suffix to a particular word occurrences in the String?

问题

output expected:

Hello PREFIX_world_SUFFIX. welcome PREFIX_world_SUFFIX java

output getting:

PREFIX_Hello world. welcome world java_SUFFIX
英文:

I am trying to add prefix and suffix to a particular occurrence of the word in the string in java. Can anyone help me and tell me where am i going wrong? Below is my code.

public static void main(String[] args) {
        String str = "Hello world. welcome world java.";
        String arr[] = str.split("[. ]");
        if(str.contains("world")) {
            System.out.println("PREFIX_"+str+"_SUFFIX");
        }
    }

output expected :

Hello PREFIX_world_SUFFIX. welcome PREFIX_world_SUFFIX java

output getting:

PREFIX_Hello world. welcome world java_SUFFIX

答案1

得分: 5

String replaced = str.replaceAll("world", "PREFIX_world_SUFFIX");
System.out.println(replaced);

英文:
String replaced = str.replaceAll("world", "PREFIX_world_SUFFIX");
System.out.println(replaced);

答案2

得分: 4

Your code is wrong since you're not changing the str variable while calling the split() function.
Also, from what I can gather, you also want to add prefix and suffix to those words containing "world".
Like if your string is something like this: "Hello worldJava! welcome to java world," you'd want to display something like this: "Hello PREFIX_worldJava_SUFFIX! welcome to java PREFIX_world_SUFFIX." (Note, the previous answers wouldn't be able to do this kind of substitution).

英文:

Your code is wrong since you're not changing the str variable while calling the split() function.
Also, from what I can gather, you also want to add prefix and suffix to those words containing "world".
Like if your string is something like this: Hello worldJava! welcome to java world, you'd want to display something like this: Hello PREFIX_worldJava_SUFFIX! welcome to java PREFIX_world_SUFFIX. (Note, the previous answers wouldn't be able to do this kind of substitution).

    String str = "Hello world. welcome world java.";
    String[] wordArr = str.split("[. ]");
    Set<String> words = new HashSet<>(Arrays.asList(wordArr));
    for (String w: words) {
        if(w.toLowerCase().contains("world")){
            str = str.replace(w, "PREFIX_"+ w +"_SUFFIX");
        }
    }
    System.out.println(str);

Note here that I am using java Set to parse unique words from the input string and then replacing them in the original string with the added prefix/suffix.

答案3

得分: 2

以下是您要翻译的内容:

只需简单地执行以下操作:

public class Example {

     public static void main(String[] args){
        String str = "Hello world. welcome world java.";
        System.out.println(str.replace("world", "PREFIX_world_SUFFIX"));
     }
}

输出:

Hello PREFIX_world_SUFFIX. welcome PREFIX_world_SUFFIX java.
英文:

Just do this simply :

public class Example {

     public static void main(String[] args){
        String str = "Hello world. welcome world java.";
        System.out.println(str.replace("world", "PREFIX_world_SUFFIX"));
     }
}

Output :

Hello PREFIX_world_SUFFIX. welcome PREFIX_world_SUFFIX java.

答案4

得分: 1

你在打印部分做得不对。你可以这样做。

String stringToCheck = "world";

if (str.contains(stringToCheck)) {
    str = str.replaceAll(stringToCheck, "PREFIX_" + stringToCheck + "_SUFFIX");
    System.out.println(str);
}
英文:

You are doing it wrong on the print side. You can do this.

String stringToCheck = "world";

if(str.contains(stringToCheck)) {
 str = str.replaceAll(stringToCheck , "PREFIX_"+stringToCheck+"_SUFFIX");
 System.out.println(str);
}

huangapple
  • 本文由 发表于 2020年8月3日 13:53:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63224368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定