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.
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)
... 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.
... 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:
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
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.
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:
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
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!"