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.

3 Answers 3

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); 
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

This won't remove characters though.
@CodeCaster: What approach would you take?
@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.
@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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.