-1

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?

I need to parse a string into 10 individual strings, each 10 characters long. 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#

string[] errorCodes = new string[10];
for (int i = 0; i < errorCodes.Length; i++)
{
    errorCodes[i] = retrievedMessage.removeFromSubstring(0, 10);
}
NealR
  • 10,189
  • 61
  • 159
  • 299
  • 4
    Can you give us an example of what the original string would look like, and what you would like the end result to be? – gwin003 Jun 05 '13 at 19:40
  • Why do you search for a single method? maybe a combination of a few string operation can do it? Any example for inputand expected output? – I4V Jun 05 '13 at 19:41
  • `substring` will return the first 10 characters, but it won't remove them from the source. If that loop were to run using substring, I'd wind up with an array of 10 strings each containing the first 10 characters of `retrievedMessage` – NealR Jun 05 '13 at 19:41
  • 1
    what is actually your question? From what I can understand you loop doesn't look right to me. Since you remove 10 characters from the string shouldn't you move by 10 not by 1? like _for (int i = 0; i < errorCodes.Length; i+=10)_ (notice the i+=10) – Rémi Jun 05 '13 at 19:42
  • Possible Duplicate: [How to get the substring in C#?](http://stackoverflow.com/q/2902394/299327) – Ryan Gates Jun 05 '13 at 19:43
  • 2
    It can't work exactly like you're asking because `String`s are immutable. It is not possible to change the original string. However, you could have a method with an `out` parameter, or have a method return a value with multiple components, and assign one of those components to the original variable. But chances are, it'll be simpler just to call Substring one last time to get the remaining characters after doing something like @Sayse suggests. – StriplingWarrior Jun 05 '13 at 19:44
  • Thanks @StriplingWarrior, that's basically what I was asking (is there a method that returns the substring requested and removes it from the original string at the same time). – NealR Jun 05 '13 at 19:46
  • 2
    @NealR since a string is immutable, you would end up with copies of the string everytime you try to "remove" the characters instead of actually modifying the string. So if you can avoid it, simply extract the values without modifying the string every time. – Tombala Jun 05 '13 at 19:49

4 Answers4

1

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();
Sayse
  • 42,633
  • 14
  • 77
  • 146
1

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); will effectively remove the first 10 characters from the original string. This way in each iteration you will be able to use the first 10 characters and assign them to the 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);
}
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
  • Depending on how often this code has to execute, you'll be creating lots of strings in memory when doing this. Especially if retrievedMessage is a long string, this could hog memory. Just a thought. – Tombala Jun 05 '13 at 19:46
  • You are right, but to my knowledge, there is no other way to do this except by using Remove which is similar solution. The better approach would be to just traverse the string based on the for loop without reassigning the substrings. I'll add that to the answer. – Nikola Davidovic Jun 05 '13 at 19:49
  • The first example is what I was looking for. Basically the shortest way to code a loop like this (was originally wondering if C# contained a method that would execute those two lines at the same time, however I guess that doesn't exist). Thanks! – NealR Jun 05 '13 at 19:53
  • @NikolaDavidovic The more efficient way to do this would be to track the last error code extraction location in retrievedMessage and do a single substring for retrievedMessage outside the loop instead of every turn of the loop. – Tombala Jun 05 '13 at 20:59
1

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[] 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
}
jordanhill123
  • 4,142
  • 2
  • 31
  • 40
0

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.

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[].

zimdanen
  • 5,508
  • 7
  • 44
  • 89