java - 如何用字符串替换索引处的单个字符?

标签 java string

我有一个字符串“speed,7,red,fast”。我想用字符串“七”替换 7。我该怎么做?

更多详细信息-

7 可以替换为任何字符串,而不仅仅是“Seven”。也可能是“七是天堂”。 我不想替换所有出现的 7。仅替换指定索引处的 7,即使用 7 的索引将 7 替换为某个字符串。

最佳答案

 replaceAll("7", "Seven") //simple as that

编辑

然后您应该查找指定的索引

 String input = "test 7 speed,7,red,fast yup 7 tr";
    int indexInteresdIn = 13;
    if(input.charAt(indexInteresdIn) == '7'){
        StringBuilder builder = new StringBuilder(input);
        builder.replace(indexInteresdIn, indexInteresdIn+1, "Seven");
        System.out.println(builder.toString());
    }

关于java - 如何用字符串替换索引处的单个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16274720/

相关文章:

java 分割字符串并只保留特定单词之后的所有内容

java - GCJ (jc1.exe) 问题

c - 如何重新分配内部和外部数组

javascript - 使用javascript中的正则表达式从文本中过滤掉数字

Java设计/实现问题

java - 如果设备是纵向的,MediaStore.ACTION_VIDEO_CAPTURE 会使相机应用程序崩溃

java - jetty Hello World 示例不编译

c - 如何声明 C 字符串数组

java - java中的句子反转

c# - 二维字符串数组中 int 值的平均数组