【问题标题】:Is there a method to switch out a letter in a String?有没有办法在字符串中切换出一个字母?
【发布时间】:2013-01-18 14:06:12
【问题描述】:

我需要一种将特定索引中的字母与另一个字母切换的方法。有类似的吗?

像这样:

String word = "test";
String letter = "e";
String secretWord = "????";

查找字母 e 的索引,然后查找 e 是否在单词中。然后切换一个“?”基于测试中e的索引。

所以secretWord 应该是?e??

【问题讨论】:

    标签: java string indexing


    【解决方案1】:

    您可以使用正则表达式来搜索和替换任何不是 letter 的字符:

    String word = "test";
    String secretWord = word.replaceAll("(?i)[^e]", "?");
    

    您还可以在正则表达式中添加更多不想替换的字母(这将替换每个非元音):

    String secretWord = word.replaceAll("(?i)[^aeiouy]", "?");
    

    正则表达式的解释:

    • (?i) 表示“不区分大小写”。
    • ^ 表示“不”。
    • aeiouy 是我们不想匹配的字符

    这是一个正则表达式替换的演示(仅使用e):

    DEMO

    【讨论】:

    • 对不起,我正在使用Java
    【解决方案2】:
            string word = "test";
            char letter = 'e';
            string secretWord = "????";
            int index = word.indexOf(letter);
            if(index >= 0)
            {
             secretWord = secretWord.substring(0,index)+letter+secretWord.substring(index + 1);
             System.out.println(secretWord);
            }
    

    这个代码是JAVA的......试试看

    【讨论】:

      【解决方案3】:

      如果您使用的是 C#,请尝试以下编码。它会帮助您

           //Declare
              string word = "test";
              char letter = 'e';
              string secretWord = "????";
              if (word.IndexOf("e") != -1)//Find the Char is found or NOT
              {
                  int index = word.IndexOf(letter); //Index of the Char
                  Console.WriteLine("Index of the Word E :" + word.IndexOf("e").ToString());
                  StringBuilder sb = new StringBuilder(secretWord);
                  sb[index] = letter; // Replacing the Char
                  secretWord = sb.ToString();
                  Console.WriteLine(secretWord);
              }
      

      【讨论】:

        【解决方案4】:

        我对任务的看法会有所不同。我们有一个秘密 ("test") 和一种显示方式,如果提供了字母 e,结果将是 "????""?e??"? 的序列本身不是字符串,而是会按需生成。然后我们不必替换字符串中的某些内容(顺便说一句,我们不能这样做,因为字符串是不可变的)。这是用代码编写的想法:

        public class SecretWord {
        
            private String secret;
        
            public SecretWord(String secret) {
                this.secret = secret;
            }
        
            public String display(char c) {
                if (secret == null) {
                    return "";
                }
                StringBuilder displayBuilder = new StringBuilder();
                for (char secretChar : secret.toCharArray()) {
                    displayBuilder.append(secretChar == c ? c : '?');
                }
                return displayBuilder.toString();
            }
        }
        

        【讨论】:

          【解决方案5】:

          我还没有足够的代表在评论中回复,但是您可以通过添加一个变量来扩展 h2ooooo 的帖子以使其灵活:

          String letter = "e";
          String secretWord = word.replaceAll("(?i)[^" + letter + "]", "?");
          

          +1,h2ooooo - 整洁的答案!

          韦斯特

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-03-04
            • 2022-06-12
            • 1970-01-01
            • 1970-01-01
            • 2014-06-02
            • 1970-01-01
            • 2023-04-07
            • 2016-09-20
            相关资源
            最近更新 更多