简体   繁体   English

使用子字符串从字符串中删除单词

[英]Remove words from string using substring

Is there a method that will remove a set amount of characters from a string, placing the removed characters into a separate string and leaving the original x amount of characters shorter? 是否有一种方法可以从字符串中删除一定数量的字符,将删除的字符放入单独的字符串中,并使原始的x个字符较短?

I need to parse a string into 10 individual strings, each 10 characters long. 我需要将一个字符串解析为10个单独的字符串,每个字符串长10个字符。 I would like to be able to do sommething simple like this, but I do not know if there is a method that works like this in C# 我希望能够像这样简单地做一些事情,但是我不知道在C#中是否有一种可以这样工作的方法

string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
    errorCodes[i] = retrievedMessage.removeFromSubstring(0, 10);
}

Edit 编辑

Now tested, seems to work fine for me 经过测试,对我来说似乎很好

        var errorCodes = "longstringgggggggggggggggggggggggggg";
        var count = 10;
        List<string> s = new List<string>();
        for (int i = 0; i < errorCodes.Length; i += count)
        {
            if (i + count > errorCodes.Length)
                count = errorCodes.Length - i;
            s.Add(errorCodes.Substring(i, count));
        }

        foreach (var str in s)
            Console.WriteLine(str);

        Console.ReadLine();

You could try this: 您可以尝试以下方法:

string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
    errorCodes[i] = retrievedMessage.Substring(0, 10);
    retrievedMessage = retrievedMessage.Substring(10);
}

The line retrievedMessage = retrievedMessage.Substring(10); 这行的retrievedMessage = retrievedMessage.Substring(10); will effectively remove the first 10 characters from the original string. 将有效地从原始字符串中删除前10个字符。 This way in each iteration you will be able to use the first 10 characters and assign them to the errorCodes[i] 这样,在每次迭代中,您将能够使用前10个字符并将其分配给errorCodes[i]

Also you could try to avoid using substrings: 您也可以尝试避免使用子字符串:

string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
    errorCodes[i] = retrievedMessage.Substring(i*10, 10);
}

This should work for you 这应该为你工作

string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
    errorCodes[i] = retrievedMessage.Substring(10*i, 10);
}

Here is an option that will remove from the string retrievedMessage 这是一个将从string retrievedMessage到的string retrievedMessage删除的选项

string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
    //option to remove from string
    errorCodes[i] = retrievedMessage.Substring(0, 10);
    retrievedMessage = retrievedMessage.Remove(0,10);  //will remove from string
}

Same basic concept as other answers but with a little checking for variable string length. 与其他答案相同的基本概念,但略微检查了可变的字符串长度。 If you know that your string is always 100 chars in length, then use one of the simpler answers. 如果您知道字符串的长度始终为100个字符,请使用以下较简单的答案之一。

string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
    int startIndex = i * 10;
    if (retrievedMessage.Length > startIndex)
    {
        int length = 10;
        if (retrievedMessage.Length < (startIndex + length))
        {
            length = retrievedMessage.Length - startIndex;
        }
        errorCodes[i] = retrievedMessage.Substring(startIndex, length);
    }
}

Note: Since errorCodes is always instantiated with a length of 10, this will have null strings if the length of retrievedMessage is <= 90. If you expect variable length, better to use a List<string> than a string[] . 注意:由于errorCodes总是以10的长度实例化,因此如果restoredMessage的长度<= 90,则它将具有空字符串。如果期望可变长度,则最好使用List<string>不是string[]

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

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