【问题标题】:Replace occurences in a String替换字符串中的出现
【发布时间】:2020-01-26 16:38:49
【问题描述】:

如何替换给定字符串的出现但没有第一次出现和最后一次出现? 输入来自键盘。 例子:

INPUT: "a creature is a small part of a big world"
        a
        the
OUTPUT: "a creature is the small part of a big world"

另一个例子:

INPUT: "a creature is a small part"
       a
       the
OUTPUT: "a creature is a small part"

在最后一个字符串保持不变,因为两个出现(即字符'a')都是第一个和最后一个。

【问题讨论】:

  • 熟悉正则表达式吗?
  • 到目前为止你有什么尝试?
  • 否则从头找第一个,从尾找第一个,把中间的都替换掉。
  • @user871611 我试图找到每个出现的索引,但我失败了。
  • 在第二个示例中,它不会替换任何内容。编写代码,将其添加到您的帖子中,并解释实际困难。

标签: java string replace substring indexof


【解决方案1】:

你可以使用String.replaceFirst(String, String):

String a = "a creature is a small part of a big world";
String b = "a";
String c = "the";
String d = a.replaceFirst(" " + b + " ", " " + c + " ");
System.out.println(d);

...打印出来:

a creature is the small part of a big world

阅读文档以获取更多信息: String documentation


编辑:

对不起,我误解了你的问题。以下是替换除第一个和最后一个之外的所有匹配项的示例:

String a = "a creature is a small part of a big world";
String b = "a";
String c = "the";

String[] array = a.split(" ");
ArrayList<Integer> occurrences = new ArrayList<>();

for (int i = 0; i < array.length; i++) {
    if (array[i].equals(b)) {
        occurrences.add(i);
    }
}

if (occurrences.size() > 0) {
    occurrences.remove(0);
}
if (occurrences.size() > 0) {
    occurrences.remove(occurrences.size() - 1);
}
for (int occurrence : occurrences) {
    array[occurrence] = c;
}

a = String.join(" ", array);
System.out.println(a);

编辑:

使用事件集合的替代类型:

String a = "a creature is a small part of a big world";
String b = "a";
String c = "the";

String[] array = a.split(" ");
Deque<Integer> occurrences = new ArrayDeque<>();

for (int i = 0; i < array.length; i++) {
    if (array[i].equals(b)) {
        occurrences.add(i);
    }
}

occurrences.pollFirst();
occurrences.pollLast();

for (int occurrence : occurrences) {
    array[occurrence] = c;
}

String d = String.join(" ", array);
System.out.println(d);

【讨论】:

  • 更好地使用 Deque 和 targetIndexes.pollFirst(); targetIndexes.pollLast();这样您就不必检查大小并弄乱索引。
【解决方案2】:
package com.example.functional;

import java.util.Arrays;
import java.util.List;
import java.util.function.UnaryOperator;

public class StringReplacementDemo {

    public static void appendString(String str){
        System.out.print(" "+str);
    }
    /**
     * @param str1
     * @param output2 
     * @param input 
     */
    public static void replaceStringExceptFistAndLastOccerance(String str1, String input, String output2) {
        List<String> list = Arrays.asList(str1.split(" "));
        int index = list.indexOf(input);
        int last = list.lastIndexOf(input);
        UnaryOperator<String> operator = t -> {
            if (t.equals(input)) {
                return output2;
            }
            return t;
        };
        list.replaceAll(operator);
        list.set(index, input);
        list.set(last, input);

        list.forEach(MainClass::appendString);
    }

    public static void main(String[] args) {

        String str1 = "a creature is a small part";
        String input = "a";
        String output ="the";
        replaceStringExceptFistAndLastOccerance(str1,input,output);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2011-04-19
    • 2016-05-07
    相关资源
    最近更新 更多