繁体   English   中英

如何基于用户输入在c#中删除字符串的特定部分?

[英]How to remove particular part of a string in c# based on userinput?

因此,我正在尝试自学C#,所以我正在做一些练习。 在本练习中,我打算创建一个输出句子的程序,用户输入要从该句子中删除哪些单词的输入。 然后打印该句子而没有该单词。

        string text = "Quick brown fox jumps over the lazy dog";
        Console.WriteLine(text);

        Console.Write("Which word would you like to remove? ");
        string userinput = Console.ReadLine();
        string newText = text.Replace(userinput, "");

        Console.WriteLine(newText);
        Console.ReadLine();

它用“”代替字符,这似乎是一种人工的,欺骗性的完成练习的方式,因为该单词被替换而不是删除。 字符之间也留有很大的空间。

有没有其他选择?

您始终可以使用string.split分割文本,该文本返回一个数组...稍后将在一个空格上连接。 检查文本是否真正包含userInput可能会很不错……然后您可以说“找不到ya dingus输入真实单词!”之类的内容。

if (text.Contains(userInput))
{
   newText = String.join(" ", text.Split(userInput));
} else ...

但是我也喜欢你的方式。 要固定双精度空格,您始终可以用单个空格替换双精度空格。

newText = newText.Replace("  ", " ");

如果您正在教自己编程 ,那么您可能要避免使用任何内置方法,例如ReplaceIndexOfContains 否则,如果您只是想学习一种新语言,那么您所使用的代码就没有问题,除了检查您删除的单词后面是否有空格。

一种无需使用内置方法即可执行所需操作的方法是,简单地“遍历”循环中的字符串,将每个字符捕获到一个临时的“ word”变量中。 当您使用空格字符时,如果您捕获的单词不是要删除的单词,则将其添加到结果字符串中。 如果字去掉,就忽视它,然后继续。 最后,除了将要删除的单词之外,您将拥有所有单词。

我在下面的示例代码中添加了注释,以更好地说明我的意思:

public static string RemoveWord(string input, string wordToRemove)
{
    // Validate aruguments, and exit early if they're null or empty
    if (string.IsNullOrEmpty(input)) return input;
    if (string.IsNullOrEmpty(wordToRemove)) return input;

    string currentWord = "";  // This will hold the current word that we're building
    string result = "";       // This will hold the string we return to the caller

    // Examine each character in the input string
    foreach (char chr in input)
    {
        // If the character is a space, then...
        if (chr == ' ')
        {
            // If the word we've captured is not the word to remove, add it to result
            if (currentWord != wordToRemove)
            {
                result += currentWord + chr;
            }

            // Reset our word to an empty string since we've added or ignored it now
            currentWord = "";
        }
        // If the character is NOT a space, then...
        else
        {
            // Add this character to the word we're building
            currentWord += chr;
        }
    }

    // When we reach the end, do one more check in case there's a word remaining
    if (currentWord != wordToRemove)
    {
        result += currentWord;
    }

    // Return the input (without the wordToRemove) to the caller
    return result;
}

用法示例:

private static void Main()
{
    string text = "Quick brown fox jumps over the lazy dog";
    string result = RemoveWord(text, "fox");

    Console.WriteLine(result);

    // Output: "Quick brown jumps over the lazy dog"

    GetKeyFromUser("\nDone! Press any key to exit...");
}

您可以使用.indexOf方法获取首次出现的替换字符串的从零开始的索引。 然后,使用该信息和替换字符串的长度,可以检查前导空格或尾随空格,并确定是否要删除它们中的一个或两个。

您的算法不错,但是您可以使用我的算法,尝试一些步骤

  1. UserInput按空格''分隔,然后ToList并存储在myWords列表中
    1. 搜索删除单词和删除单词,如果通过myWords.reomve(item)匹配单词删除单词,则使用foreach并设置条件
    2. 列表连接后,使用string.join(““,myWords); **您了解或需要代码以获取更多详细信息? **

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM