【问题标题】:How to remove one charachter from a string如何从字符串中删除一个字符
【发布时间】:2020-04-11 20:55:44
【问题描述】:

我想通过扫描仪读取一个字符的索引,然后将其从字符串中删除。只有一个问题:如果 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);
  }
}

【问题讨论】:

  • " while(f
  • 我不知道您的问题与您的代码有何关系。您想替换第一个t,然后是第二个t,依此类推,目前(您声称)它替换了所有t(s)。 如何?您不要在此代码中使用 replace。我跑了它。如果您删除无限的while 循环并输入t,您会得到:3\n6\n9\nTexty 文本(其中\n 是换行符)。你想要什么

标签: java


【解决方案1】:

你可以使用replaceFirst:

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

【讨论】:

    【解决方案2】:

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

    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】:

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

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

      【讨论】:

        【解决方案4】:

        由于您要指定索引,因此您可能希望替换特定字符的第二个。这只是通过忽略它之前的那些来实现的。这将返回一个 Optional&lt;String&gt; 来封装结果。在适当的情况下抛出异常。

            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));
            }
        

        【讨论】:

          猜你喜欢
          • 2014-07-30
          • 2023-03-14
          • 2021-12-11
          • 2011-05-16
          • 2011-11-18
          • 2017-07-19
          • 2021-04-06
          • 1970-01-01
          相关资源
          最近更新 更多