如何从字符串中删除一个字符

时间:2020-04-11 20:55:44

标签: java

我要使用扫描仪读取char的索引,然后将其从字符串中删除。仅有一个问题:如果char在字符串中多次出现,则.replace()会删除所有字符。

例如,我想从字符串“ Texty text”中获取第一个“ t”的索引,然后仅删除该“ t”。然后我要获取第二个“ t”的索引,然后将其删除。

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
   String text = "Texty text";
   Scanner sc = new Scanner(System.in);
   int f = 0;
   int x = 0;
   while(f<1){
   char c = sc.next().charAt(0);
    for(int i = 0; i<text.length();i++){

      if(text.charAt(i)==c){

        System.out.println(x);
        x++;
      }
      else{
        x++;
      }

    }
  }
   System.out.println(text);
  }
}

4 个答案:

答案 0 :(得分:1)

您可以使用replaceFirst

System.out.println(text.replaceFirst("t", ""));

答案 1 :(得分:0)

尝试使用txt.substring(x,y)

x =通常为0,但x是第一个起始索引 y =这是您要删除的内容,例如,为字符串的最后一个单词编写以下代码: txt.substring(0,txt.length()-1)

答案 2 :(得分:0)

可能您正在寻找类似以下的内容:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String text = "Texty text";
        String copy = text;
        Scanner sc = new Scanner(System.in);
        while (true) {
            text = copy;
            System.out.print("Enter a character from '" + text + "': ");
            char c = sc.next().charAt(0);
            for (int i = 0; i < text.length(); i++) {
                if (Character.toUpperCase(text.charAt(i)) == Character.toUpperCase(c)) {
                    System.out.println(c + " was found at " + i);
                    text = text.substring(0, i) + "%" + text.substring(i + 1);
                    System.out.println("After replacing " + c + " with % at " + i + ": " + text);
                }
            }
        }
    }
}

示例运行:

Enter a character from 'Texty text': x
x was found at 2
After replacing x with % at 2: Te%ty text
x was found at 8
After replacing x with % at 8: Te%ty te%t
Enter a character from 'Texty text': t
t was found at 0
After replacing t with % at 0: %exty text
t was found at 3
After replacing t with % at 3: %ex%y text
t was found at 6
After replacing t with % at 6: %ex%y %ext
t was found at 9
After replacing t with % at 9: %ex%y %ex%
Enter a character from 'Texty text': 

答案 3 :(得分:0)

由于要指定索引,因此可能需要替换特定字符的第二个字符。这只是通过忽略之前的内容来实现的。这将返回Optional<String>来包围结果。在适当情况下会引发异常。

    public static void main(String[] args) {
        // Replace the first i
        Optional<String> opt = replace("This is a testi", 1, "i", "z");
        // Replace the second i (and so on).
        System.out.println(opt.get());
        opt = replace("This is a testi", 2, "i", "z");
        System.out.println(opt.get());
        opt = replace("This is a testi", 3, "i", "z");
        System.out.println(opt.get());
        opt = replace("This is a testi", 4, "i", "z");
        System.out.println(opt.get());
    }

    public static Optional<String> replace(String str, int occurrence, String find, String repl) {
        if (occurrence == 0) {
            throw new IllegalArgumentException("occurrence <= 0");
        }
        int i = -1;
        String strCopy = str;
        while (occurrence-- > 0) {
            i = str.indexOf(find, i+1);
            if (i < 0) {
                throw new IllegalStateException("insufficient occurrences of '" + find + "'");
            }
        }
        str = str.substring(0,i);
        return Optional.of(str + strCopy.substring(i).replaceFirst(find, repl));
    }
相关问题