How to remove all occurrences of a character / substring?

I am using the .NET Micro Framework 4.1, which does not implement methods Regexor String.Replace/ String.Removeas far as I know.

I have a line defined as:

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

Is there a way to remove all occurrences [esc]vfrom this line? Where is the escape character ( 0x1B) used followed by 0x76in NetMF?

This, hopefully, will leave me:

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

I thought about the possibility of using the method String.Split(), but it seems too memory intensive, since the code runs on a small NETMF memory.

+4
source share
3 answers

Use

StringBuilder.Replace
StringBuilder.Remove 

.NET Micro Framework 2.5, 3.0, 4.0 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();
        } 

:

        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);
+2

?

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);
}
+2

, -.

, @Filip answer:

:

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

,

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

byte[].


, ( - , , , , ), "" , :

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

, "" , "v" .


, , char/substring, .

+1
source

All Articles