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

Replacing texst in strings


  • Please log in to reply
10 replies to this topic

#1 bulletprooffool

bulletprooffool

    Member

  • Members
  • PipPip
  • 12 posts

Posted 13 December 2011 - 07:57 PM

Hi, this is a quick question,

I am quite new to most of this and am having a little difficulty with the MicroFramework.

I am building a project to pull info off the web and display it - I am able to gather info and am able to parse info now, but the one bit I text that I gather is surrounded by white space.
I have found the NetMF 4.2 replace, but my string is returned with some line breaks and I am unsure how to remove line breaks from my string.

If anyone knows this off the top of your head, please fire it my way.

I have tried:

 tagContent = replace(tagContent, "\n", "");
 tagContent = replace(tagContent, "\r", "");


using:


  public static string replace(string str, string unwanted, string replacement)
       {
           StringBuilder sb = new StringBuilder(str);
           sb.Replace(unwanted, replacement);
           return sb.ToString();
       }


Thanks
bpf

#2 bulletprooffool

bulletprooffool

    Member

  • Members
  • PipPip
  • 12 posts

Posted 13 December 2011 - 08:33 PM

Got it - thanks.

Dropped a breakpoint, rechecked and found the return was skewed by a whole lot of tabs (\t)

Code now says:
 tagContent = replace(tagContent, "\n", "");
  tagContent = replace(tagContent, "\r", "");
 tagContent = replace(tagContent, "\t", "");

Seems to do what it says on the tin now . .

#3 dottorduino

dottorduino

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationCatania, Italy

Posted 14 May 2012 - 02:51 PM

Hi folks, I can't find String.Replace in my NetDuino Plus project. This is the error: "String does not contain a defenition for 'Replace' and no extension method...bla bla bla" I use .Net MF 4.1

#4 nicolastintorre

nicolastintorre

    New Member

  • Members
  • Pip
  • 5 posts

Posted 24 July 2012 - 12:52 PM

Hi All, When I use the function Replace(unwanted, replacement) of stringBuilder, if the "replacement" string length is bigger than "unwanted" length then the method throws "OutOfRangeException". I have microframework 4.2. Thanks in advance.

#5 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 24 August 2012 - 08:40 AM

Hi folks,
I can't find String.Replace in my NetDuino Plus project. This is the error:
"String does not contain a defenition for 'Replace' and no extension method...bla bla bla"

I use .Net MF 4.1


You cant actually do a <string>.Replace(), as strings don't have that method in the micro framework.

You can, however, do what bulletprooffool did and roll your own Replace() function, and call it wherever you need it. I like to have a utils.cs class to keep this kind of crap handy.

[OR]

If you don't want to get that fancy/reusable with it, you can just do it inline-ish by doing exactly what is in that function; make the String a StringBuilder, do your fiddling, and then assign it back to your original gangster String. That will look a little something like this:
String tacoString = "I like Tacos";
// "I like Tacos"
StringBuilder  tacoStringBuilder = new StringBuilder(tacoString);
tacoString = tacoStringBuilder.Replace("like","love").ToString();
// "I love Tacos"

Just be sure you get that last .ToString() in there; I don't think it will compile if you don't.

Hope this helps.

Mark

#6 marky-b

marky-b

    Member

  • Members
  • PipPip
  • 17 posts

Posted 24 August 2012 - 08:45 AM

Hi All,
When I use the function Replace(unwanted, replacement) of stringBuilder, if the "replacement" string length is bigger than "unwanted" length then the method throws "OutOfRangeException". I have microframework 4.2.
Thanks in advance.


I haven't had issues with the replacement text being longer than what it is replacing.

I found a pretty interesting bug back when 4.2 was still alpha-ish; outlined here

Have you tried with a shorter base string? You could possibly be running out of memory on the netduino. Try and compile a simple .NETMF project and run it locally on your PC. If it works there, but not on your netduino, I'm guessing you're asking a bit too much from it.

Possible workaround: Look at an example on the thread I linked above (first post in that thread, iirc) that reads through one char at a time and does the replacing that way. It was Valkyrie-MT's example. [WARNING: It's not pretty] ;)

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 24 August 2012 - 01:14 PM

One other thought: .NET Micro Framework supports extension methods. So you may be able to extend the String class in .NET Micro Framework and use your desktop .NET code as-is. Chris

#8 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 26 November 2012 - 05:03 PM

Hi there,

I'm trying to post a date/time via an HTTP request for an application I'm writing. The only problem is that I am getting an error because of the space in the date/time.

string = "&dateTimeStamp=06/01/2011 00:00:28"


I need it to be

string = "&dateTimeStamp=06/01/2011%2000:00:28"


I have created the following:

public static string replace(string str, string unwanted, string replacement)
        {
            StringBuilder sb = new StringBuilder(str);
            sb.Replace(unwanted, replacement);
            return sb.ToString();
        }

but calling it like this does not get rid of the space:

string httpLine = replace(originalLine, " ", "%20");

Is there an escape character I should use for replacing the space?

Thanks,
Donovan

#9 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 26 November 2012 - 05:26 PM

Hi there,

I'm trying to post a date/time via an HTTP request for an application I'm writing. The only problem is that I am getting an error because of the space in the date/time.



I need it to be



I have created the following:

public static string replace(string str, string unwanted, string replacement)
        {
            StringBuilder sb = new StringBuilder(str);
            sb.Replace(unwanted, replacement);
            return sb.ToString();
        }

but calling it like this does not get rid of the space:

string httpLine = replace(originalLine, " ", "%20");

Is there an escape character I should use for replacing the space?

Thanks,
Donovan


What version of the framework and firmware are you using. I tried this using 4.2.0.1 and it replaced the space however it messed up the time. If my time was "11/26/2012 00:00:00" after the replace it showed as "/211/26/201%2000:00:00". Probably a bug in the framework.

#10 Nicky

Nicky

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationDenmark

Posted 26 November 2012 - 05:38 PM

I usually make an extension:


public static class Extensions 
    {
        public static string Replace(this string str, string what, string with)
        {
            int index = -1;

            while ((index = str.IndexOf(what)) != -1)
            {
                if (index > 0)
                {
                    str = str.Substring(0, index) + with + str.Substring(index + what.Length);
                }
            }

            return str;
        }
    }

And then uses it by:

string str = "something";
str.Replace("\r", "");

ntools
TCP Listener (Beta) · FTP Server (Alpha)
Netduino Plus Go Module · Xml Parser
http://ntools.codeplex.com/


#11 StefanUSMC

StefanUSMC

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • LocationWisconsin, USA

Posted 27 November 2012 - 04:19 AM

I came across a codeplex project called .NET Micro Framework - Common Extensions that adds a few helpful extension methods, including string.replace. I have used a few of them, and have not had any issues with using them on NetMF 4.2. Hopefully it is as helpful for you as it was for me.

http://netmfcommonext.codeplex.com/




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.