How to remove all occurrences of a character/substring?
Asked Answered
R

3

5

I am using the .NET Micro Framework 4.1, which does no implement the Regex class or the String.Replace / String.Remove methods as far as I'm aware.

I have a string defined as:

string message = "[esc]vI AM A STRING. [esc]vI AM A STRING AND DO LOTS OF THINGS...";

Is there a way of removing all the occurrences of [esc]v from this string? Where the escape character is used (0x1B) followed by 0x76 in NetMF?

This would hopefully leave me with:

string message = "I AM A STRING. I AM A STRING AND DO LOTS OF THINGS...";

I've thought of possibly using the String.Split() method, but this seems too memory-demanding, as the code is running on a small-memoried NETMF board.

Rauwolfia answered 15/1, 2015 at 13:13 Comment(0)
N
4

Use

StringBuilder.Replace
StringBuilder.Remove 

which are available in the .NET Micro Framework versions 2.5, 3.0, 4.0, and 4.1.

        public static string fixStr(string message, char c)
        {
          StringBuilder aStr = new StringBuilder(message);
          for (int i = 0; i < aStr.Length; i++)
          {
            if (aStr[i] == c)
            {
                aStr.Remove(i, 1);
            }
          }
          return aStr.ToString();
        } 

Usage:

        string message = "" + (char)0x1B + (char)0x76 + "I AM A STRING. " + (char)0x1B + (char)0x76 + "I AM A STRING AND DO LOTS OF THINGS...";

        message = fixStr(message, (char)0x76);
        message = fixStr(message, (char)0x1B);
Nahshun answered 15/1, 2015 at 13:32 Comment(0)
J
2

Extensions ? Try this

public static string Replace(this string stringToSearch, char charToFind, char charToSubstitute)
{        
    char[] chars = stringToSearch.ToCharArray();
    for (int i = 0; i < chars.Length; i++)
        if (chars[i] == charToFind) chars[i] = charToSubstitute;

    return new string(chars);
}
Jala answered 15/1, 2015 at 13:26 Comment(4)
This won't remove characters though.Seneca
@CodeCaster: What approach would you take?Rauwolfia
@jbutler I'm not quite a limited resources expert, so I'd just create a new array of the same size, copy valid characters there and keep a counter for the new length. Or keep a removedCharsCount and do chars[i - removedCharsCount] = chars[i], increasing that counter every time a character to remove is encountered. But see also Fastest way to remove chars from string.Seneca
@CodeCaster: Looks to be fast, just pretty heavy on memory (as you said). Looks like i'll take an approach similar to this and see what I can come up with.Rauwolfia
R
1

I was eventually able to find out how to achieve this another way.

However, I first used a method similar to @Filip's answer:

I used:

String message = "[esc]vI AM A STRING. [esc]vI AM A STRING AND DO LOTS OF THINGS...";

byte [] msg = System.Text.Encoding.UTF8.GetBytes(message);
for(int i=0; i< msg.Length; i++)
{
    if (msg[i] ==0x1B || msg[i] == 0x76) msg[i] = 0x00;
}
//msg is now in byte[] format

I could then have continued to reconvert this into my string by using the

message = new string(System.Text.Encoding.UTF8.getChars(msg));

Although, for my project, I was able to leave it in byte[] format.


However, in light of my situation, (since I was reading from a serial port - not mentioned in question, I know as didn't think this was important), I was able to simply 'stop' these characters from being entered into the string in the first place, using:

if( buffer[0] !=0x1B && buffer[0] !=0x76)
{
    //add to string since it's not either
}

I was able to do this since the 'wanted' characters were all in upper case, and so 'v' would never appear in the message.


However, I would still be interested if there was a better way of removing a char/substring if the need arised in the future.

Rauwolfia answered 15/1, 2015 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.