【问题标题】:Find index locations of a specified letter in a string查找字符串中指定字母的索引位置
【发布时间】:2018-01-23 10:12:58
【问题描述】:

如果我第一次没有解释清楚,我深表歉意。我在下面用粗体进一步编辑了我的解释。

在下面的程序中,用户输入一个单词,然后输入一个用户想用任何字符替换的字母。例如,用户输入一个单词“Hello”,替换字母是“l”和“$”。所以“Hello”会变成“He$$o”。 首先,目标是找到“l”的位置(例如 - 2,3),然后替换该特定位置的元素。

我首先找到“l”的位置并将其存储在 findIndex 数组中。每次我运行程序时,我都会将“22222”存储在 findIndex[] 数组中。在这一点上,我什至不确定我是否应用了正确的逻辑。任何建议将被认真考虑!请不要使用 LINQ。

public static void RemoveSpecifiedCharacters()
    {
        Console.WriteLine("\nWrite a word/sentence: ");
        string myString = Console.ReadLine();

        Console.Write("Type the character you would like to replace: ");
        string myCharacter = Console.ReadLine();
        int[] findIndex = new int[myString.Length];

        for (int i = 0; i < myString.Length; i++)
        {

            findIndex[i] = myString.IndexOf(myCharacter, 0);

        }

        for (int i = 0; i < findIndex.Length; i++)
        {
            Console.Write(findIndex[i]);
        }           
    }

【问题讨论】:

  • 这是你的目标吗? user enter's a word "Hello" and the replacement letter is "l" with "$". So "Hello" will become "He$$o". 然后试试String.Replace()
  • IndexOf 方法的第二个参数是搜索的起始索引 - 您总是从索引 0 开始,所以它总是会在第三个字符 (index=2) 处找到“l”。目前尚不清楚您在第一个循环中实际尝试做什么。您是否尝试创建角色出现的位置数组?如果这是您想要的,那么这里的示例可能会对您有所帮助:msdn.microsoft.com/en-us/library/…

标签: c# .net arrays string


【解决方案1】:

这可能是你想要的:

public static void RemoveSpecifiedCharacters()
{
    Console.WriteLine("\nWrite a word/sentence: ");
    string myString = Console.ReadLine();

    Console.Write("Type the character you would like to replace: ");
    string myCharacter = Console.ReadLine();
    List<int> findIndex = new List<int>();

    int offs = 0;
    while (offs < myString.Length)
    {
      offs = myString.IndexOf(myCharacter, offs);;
      if (offs == -1)
        break;
      findIndex.Add(offs);
      offs++;
    }

    for (int i = 0; i < findIndex.Count; i++)
    {
        Console.Write(findIndex[i]);
    }           
}

为字符串的开头设置一个初始偏移量,如果找不到则尝试找到所需字符的索引退出,否则存储位置并增加偏移量,以便下一个循环在找到的位置之后开始。然后继续循环。

由于您不知道会找到多少个字符,因此列表比数组更好地存储结果。之后总是可以使用 .ToArray() 将其转换为数组。

【讨论】:

  • 这很棒。非常感谢!
【解决方案2】:

以下应该达到目的:

var str = "Hello";
var replaced = str.Replace('l', '$');

【讨论】:

  • 题目是“查找字符串中指定字母的索引位置”。您的代码将进行替换,但不回答问题。
  • 可能你需要细化你的问题:“在下面的程序中,用户输入一个单词,然后输入一个用户想用任何字符替换的字母。例如,用户输入的单词“Hello”,替换字母是“l”和“$”。所以“Hello”将变成“He$$o”
  • 由于 OP 的代码没有尝试替换字母,所以问题可能是正确的。
【解决方案3】:

尽管使用String.Replace 更容易,但我只是想解释一下为什么会得到 [2,2,2,2,2] 数组。

首先,IndexOf方法返回字符第一次出现的索引,从0开始。

其次,您正在使用方法重载IndexOf(myCharacter, 0),它“说”字符搜索应始终从字符串的开头进行。

为了规避这个问题,你应该使用IndexOf(myCharacter, i, 1)来设置搜索从第i个字符开始,而不是字符串的开始。

【讨论】:

  • 您需要修改循环 - 否则输出为 2 2 2 3 -1
  • 我更新了答案,所以基本上 IndexOf 只会检查一个字符
  • 现在结果是 -1 -1 2 3 -1 - 很确定 OP 也不想要这样。
  • 我们需要等待OP的回复:)
  • 我知道我们的等待已经结束了
【解决方案4】:

我想一个简单的解决方案是将字符串拆分为字符数组,然后进行比较? 例如:

        Console.WriteLine("\nWrite a word/sentence: ");
        char[] myString = Console.ReadLine().ToCharArray();

        Console.Write("Type the character you would like to replace: ");
        char myCharacter = Console.ReadLine().ToCharArray()[0];
        int[] findIndex = new int[myString.Length];
        int indexCount = 0;
        for (int i = 0; i < myString.Length; i++)
        {
            if (myString[i] == myCharacter)
                findIndex[indexCount++] = i;

        }

        for (int i = 0; i < indexCount; i++)
        {
            Console.Write(findIndex[i]);
        }

【讨论】:

  • 它会按预期打印索引 0 吗?如果没有搜索匹配,它不会打印任何索引(甚至不是 0)。
  • Nothing 查看最后一个循环。
  • 抱歉,错过了。
猜你喜欢
  • 2012-05-29
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多