Java 替换字符串中的事件

Java 替换字符串中的事件,java,string,replace,substring,indexof,Java,String,Replace,Substring,Indexof,如何替换给定字符串的出现,但没有第一次出现和最后一次出现? 输入来自键盘。 示例: 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 creatu

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

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”)都是第一个和最后一个。

您可以使用
String。replaceFirst(String,String)

String a=“生物是大世界的一小部分”;
字符串b=“a”;
String c=“the”;
字符串d=a.replaceFirst(“+b+”,“+c+”);
系统输出打印ln(d);
。。。打印出:

a creature is the small part of a big world
有关更多信息,请阅读文档:


编辑:

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

String a=“生物是大世界的一小部分”;
字符串b=“a”;
String c=“the”;
字符串[]数组=a.split(“”);
ArrayList引用=新建ArrayList();
for(int i=0;i0){
删除(0);
}
如果(引用次数.size()>0){
引用.删除(引用.大小()-1);
}
for(int引用:引用){
数组[出现]=c;
}
a=字符串.join(“,数组);
系统输出打印项次(a);

编辑:

使用引用集合的替代类型:

String a=“生物是大世界的一小部分”;
字符串b=“a”;
String c=“the”;
字符串[]数组=a.split(“”);
Deque引用=新的ARRAYDEFUE();
for(int i=0;i
package com.example.functional;
导入java.util.array;
导入java.util.List;
导入java.util.function.UnaryOperator;
公共类StringReplacementDemo{
公共静态无效追加字符串(字符串str){
系统输出打印(“+str”);
}
/**
*@param str1
*@param output2
*@param输入
*/
公共静态void replaceStringExceptFistAndLastOccerance(字符串str1、字符串输入、字符串输出2){
List=Arrays.asList(str1.split(“”);
int index=list.indexOf(输入);
int last=list.lastIndexOf(输入);
一元运算符=t->{
如果(t等于(输入)){
返回输出2;
}
返回t;
};
list.replaceAll(操作员);
列表.集合(索引,输入);
列表.设置(最后,输入);
list.forEach(MainClass::appendString);
}
公共静态void main(字符串[]args){
String str1=“生物是一小部分”;
字符串输入=“a”;
String output=“the”;
replaceStringExceptFistAndLastOccerance(str1,输入,输出);
}
}

您熟悉正则表达式吗?到目前为止您尝试了什么?否则,请从开头查找第一个,从结尾查找第一个,并替换中间的所有内容。@user871611我已尝试查找每次出现的索引,但失败了。在第二个示例中,它将不替换任何内容。编写代码,将其添加到文章中,并解释实际困难;targetIndexes.pollLast();这样,您就不必检查索引的大小和混乱。
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);
    }
}