Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

String manipulation in .net 4.1 Micro


  • Please log in to reply
12 replies to this topic

#1 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 13 October 2011 - 01:04 AM

I am attempting to do some string manipulation (below), but I came to find that some of my favorite methods and types aren't supported by .NETMF :(

I first attempted:

string output = string.Format("It is {0} {1} {2} {3}", str0, str1, str2, str3);
but this clearly failed, as string.Format is missing from 4.1.

I then decided I was going to try and roll my own extension method, but came to find out that stringbuilder (more specifically "replace") is missing as well.

I see that Valkyrie-MT as an extension method class he uses for some of his projects, which includes:

public static string Replace(this string stringToSearch, char charToFind, char charToSubstitute)
{
    // Surely there must be nicer way than this?
    char[] chars = stringToSearch.ToCharArray();
    for (int i = 0; i < chars.Length; i++)
        if (chars[i] == charToFind) chars[i] = charToSubstitute;

    return new string(chars);
}
but I would have to think there is a better way to do this in .NETMF (no offense Valkyrie) ;)

Any thoughts? Should I be looking at 4.2?

Mark

#2 Valkyrie-MT

Valkyrie-MT

    Advanced Member

  • Members
  • PipPipPip
  • 315 posts
  • LocationIndiana, USA

Posted 13 October 2011 - 02:01 PM

... but I would have to think there is a better way to do this in .NETMF (no offense Valkyrie) ;)

Any thoughts? Should I be looking at 4.2?


Ha, no offense taken. I fully admit that method could be better. Welcome to the MICRO Framework! First off, version 4.2 DOES add Stringbuilder (don't know about replace), but generally, this is the reality of the Micro Framework. It has to fit in an impossibly small amount of memory, so these tradeoff have to be made. There are other libraries scattered in Codeplex that offer more functionality, but you should be very mindful of the RAM implications! It is very easy to code as if you are on the Desktop .NET Framework and waste a ton of RAM. And before you know it, you will be getting memory exceptions.

-Valkyrie-MT

#3 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 13 October 2011 - 02:49 PM

... It is very easy to code as if you are on the Desktop .NET Framework and waste a ton of RAM. And before you know it, you will be getting memory exceptions.


I have some spare SODIMM here. Can't I just slap them together with some electrical tape?

I just installed the 4.2 SDK and found some sunshine and marshmallows included:

Posted Image

mmmm

Mark

#4 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 13 October 2011 - 08:08 PM

So I tried rolling out a method to essentially do what string.Format() does in the full framework, and experienced some weird results.

First, My code:

Method
public static string StringFormat(string baseString, object[] objectArray)
{
    StringBuilder sb = new StringBuilder(baseString);
    for (int i = 0; i < objectArray.Length; i++)
        sb.Replace("{" + i.ToString() + "}", objectArray[i].ToString());
    return sb.ToString();
}

Called by
string baseString = "My car is a {0} {1} {2}";
object[] carAttribs = { "blue", "Subaru", "Impreza" };
string output = Utils.StringFormat(baseString, carAttribs);

Should output:
My car is a blue Subaru Impreza

But instead outputs:
ycarar iMy car is ablSubImpreza

I've been staring at it for a couple of hours, and even tried using different short variable wrappers ({0}, %%0%%, ^^0^^)to no avail.

The only way i could get it to work is if i tried some crazy ridiculous wrapper like ^%%^{0}^%%^.

Any suggestions on a better variable wrapper?

Mark

Edit: I have also confirmed that breaking it down to it's barebones, I am getting the same results.
StringBuilder sb = new StringBuilder("My car is a a0a a1a a2a");
sb.Replace("a0a", "blue");
sb.Replace("a1a", "Subaru");
sb.Replace("a2a", "Impreza");
string output2 = sb.ToString();
Spits out:
ycarar iMy car is ablSubImpreza


#5 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 14 October 2011 - 02:23 PM

Im starting to think that maybe stringbuilders dont automatically grow when you .Replace(). I'll report back what i find. Mark

#6 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 14 October 2011 - 03:46 PM

Im starting to think that maybe stringbuilders dont automatically grow when you .Replace().


Never mind, it looks like they grow just fine, but just cant figure out how to .Replace() correctly.

The frustration mounts.

Mark

#7 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 15 October 2011 - 07:01 PM

In 4.2 this works just fine for me,
Dim before As String = "This my string"
Dim after As String = String.Empty

after = replace(before, "my", "is substituted in")

Debug.Print("before --> " & before)
Debug.Print("after replacement --> " & after)  

before --> This my string
after replacement --> This is substituted in string

Public Function replace(ByVal str As String,
                            ByVal unwanted As String,
                            ByVal replacement As String) As String
        Dim sb As New System.Text.StringBuilder(str)
        sb.Replace(unwanted, replacement)
        Return (sb.ToString)
End Function
Baxter

#8 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 17 October 2011 - 03:13 PM

In 4.2 this works just fine for me,

[VBCodeHere]
Baxter


I ported to c#:
string before = "This my string";
string after = string.Empty;

after = replace(before, "my", "is substituted in");

Debug.Print("before --> " + before);
Debug.Print("after replacement --> " + after);
public static string replace(string str, string unwanted, string replacement)
{
    StringBuilder sb = new StringBuilder(str);
    sb.Replace(unwanted,replacement);
    return sb.ToString();
}
before --> This my string
after replacement --> This is substituted in string




Just doing a single replace works as intended, but when you do multiple replaces in a row, things start to look funky..
after = replace(before, "my", "is substituted in");
after = replace(after, "string", "cant touch this");

before --> This my string
after replacement --> ubstituteThis is substicant touch this


#9 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 17 October 2011 - 06:26 PM

Looking at this,
StringBuilder sb = new StringBuilder("My car is a a0a a1a a2a"); 
sb.Replace("a0a", "blue"); 
sb.Replace("a1a", "Subaru"); 
sb.Replace("a2a", "Impreza"); 
string output2 = sb.ToString();
I think you are calling replace incorrectly. Try using the replace function this way,
String replaced = replace("My car is a a0a a1a a2a","a0a", "blue");
replaced = replace(replaced, "a1a", "Subaru");
... etc
Baxter

#10 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 17 October 2011 - 08:19 PM

Looking at this,

StringBuilder sb = new StringBuilder("My car is a a0a a1a a2a"); 
sb.Replace("a0a", "blue"); 
sb.Replace("a1a", "Subaru"); 
sb.Replace("a2a", "Impreza"); 
string output2 = sb.ToString();
I think you are calling replace incorrectly. Try using the replace function this way,
String replaced = replace("My car is a a0a a1a a2a","a0a", "blue");
replaced = replace(replaced, "a1a", "Subaru");
... etc
Baxter


Code with results:
String replaced = replace("My car is a a0a a1a a2a", "a0a", "blue");
Debug.Print(replaced); // replaced:  yMy car is ablue a1a a2a
replaced = replace(replaced, "a1a", "Subaru");
Debug.Print(replaced); // replaced:  cayMy car is ablSubaru a2a

Try it for yourself.

Also, I sent a question to Julius Friedman on CodePlex. He's the dev that added StringBuilder to .NETMF 4.2. I'm curious what he will say.

#11 Corey Kosak

Corey Kosak

    Advanced Member

  • Members
  • PipPipPip
  • 276 posts
  • LocationHoboken, NJ

Posted 18 October 2011 - 01:34 AM

Wow, it's pretty bad. It appears to me that it works OK when the length of the replacement string is less than or equal to that of the source string; otherwise it does this funky rotation+mangling:

using System.Text;
using Microsoft.SPOT;

namespace NetduinoApplication2 {
  public class Program {
    public static void Main() {
      const string baseString = "01234567890xxxxxYYYYY";
      Test(baseString, "a");
      Test(baseString, "aa");
      Test(baseString, "aaa");
      Test(baseString, "aaaa");
      Test(baseString, "aaaaa");
      Test(baseString, "aaaaaa");
      Test(baseString, "aaaaaaa");
      Test(baseString, "aaaaaaaa");
    }

    public static void Test(string baseString, string replace) {
      var sb=new StringBuilder(baseString);
      sb.Replace("xxx", replace);
      Debug.Print(sb.ToString());
    }
  }
}

The first three are correct, then we go into the Twilight Zone:
01234567890axxYYYYY
01234567890aaxxYYYYY
01234567890aaaxxYYYYY
10123456789aaaaxxYYYYY
23012345678aaaaaxxYYYYY
34501234567aaaaaaxxYYYYY
45670123456aaaaaaaxxYYYYY
56789012345aaaaaaaaxxYYYYY


#12 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 18 October 2011 - 03:22 AM

From Julius: "Hey I just wanted to say I have confirmed the bug and assigned myself to the issue. I will be taking care of this bug very shortly! I am very sorry for any inconvenience this has caused. Thank you for the kind words and for taking the time to find it!" yay! You can follow the issue here: http://netmf.codeple...m/workitem/1313 Thanks for all your input everyone! Mark

#13 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 18 October 2011 - 04:36 AM

Patched.. "This bug has been corrected with patch #10623. Please review the patch for more information. http://netmf.codeple...ol/list/patches This issue will now be closed!"




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.