DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Need help with some string manip in C#  (Read 9,279 times)

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Need help with some string manip in C#
« on: June 21, 2014, 10:54 PM »
Maybe someone can help me with this. I'm needing C# code that reliably does the following: given a string of characters, it returns a substring of characters. If the substring has an improperly formatted decimal number, that is the returned substring. Otherwise it returns a zero-length string.

So, as examples, it would detect the following in string "abc3. 04xta": -> returns "3. 04"

And for this string: "3bcd4 0.  25z10": returns "4 0.  25"

What I'm ultimately wanting is for the code to do a string.replace( ) that fixes the misformatting:

"abc3. 04xtra" becomes "abc3.04xtra", and "3bcd4 0.  25z10" becomes "3bcd40.25z10".

I know regular expressions is probably the way to go, but I'm having trouble getting my head around them.

The method I'd like would either return the poorly formatted "decimal number" substring; or, optionally,
it could fix the formatting in that substring and replace all occurrences in the larger string.

The end result is that it would turn this sentence...

"The bread was $3 .49, and it had always been $2 .15 before."

...into the following...

"The bread was $3.49, and it had always been $2.15 before."
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
Re: Need help with some string manip in C#
« Reply #1 on: June 21, 2014, 11:45 PM »
What constitutes an "an improperly formatted decimal number"?  Is it a decimal number that contains spaces (or whitespace characters)?

What would you want to be returned for the following:

    "abc3.-04xta"

or

    "abc6/. 7?8xta"

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Re: Need help with some string manip in C#
« Reply #2 on: June 21, 2014, 11:59 PM »
For my purposes, an improperly formatted decimal number is any substring which, when any interleaved whitespace or other non-decimal, non alphanumeric characters are removed, forms a decimal number.

"abc3.-04xta" becomes "abc3.04xta"

"abc6/. 7?8xta" becomes "abc6.78xta"
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Need help with some string manip in C#
« Reply #3 on: June 22, 2014, 12:06 AM »
Does this case present a problem for you? (It is properly formatted.)

The fleet consists of 10 2,000-TEU vessels.

If it does, I think you are in for a world of hurt in deciding how things get resolved.

Also, this case:

The apples were handed out, and John got 10. 5 of them were rotten.

But, just off the cuff, does this pattern work?

([0-9 ]+)\.([0-9 ]+)

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Re: Need help with some string manip in C#
« Reply #4 on: June 22, 2014, 12:13 AM »
That looks workable,  Ren. But any regex would need to capture and replace whitespace before,  among and after the numbers: "9. 07" becomes "9.07", and " 7  6 . 04" becomes "76.04"
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
Re: Need help with some string manip in C#
« Reply #5 on: June 22, 2014, 01:09 AM »
I think the following algorithm will get you what you want (or close):

 - remove any characters that are not an alpha, a digit, or a decimal point
 - replace runs of alpha characters with a space

Now you have a space-delimited string of the numbers you're looking for.

Some things that might need work or modification:

 - if you want to allow negative numeric values, then you might need to handle the minus sign specially (you discarded it in my question's example, so I figure you're not interested in negative values)
 - if you want to allow a comma to be used as a decimal separator

But I think the basic idea gets you what you're looking for.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Need help with some string manip in C#
« Reply #6 on: June 22, 2014, 02:19 AM »
I think a regex is the way to go.  Easy to make one that ignores whitespaces, etc.

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Need help with some string manip in C#
« Reply #7 on: June 22, 2014, 02:55 AM »
That looks workable,  Ren. But any regex would need to capture and replace whitespace before,  among and after the numbers: "9. 07" becomes "9.07", and " 7  6 . 04" becomes "76.04"

While you can do something like:

( )?([0-9 ]+)\.([0-9 ]+)( )?

You can also concat the capture groups and use string.Trim() or string.Replace(). That's simple and reliable.


Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Re: Need help with some string manip in C#
« Reply #8 on: June 22, 2014, 03:02 AM »
Yeah, I installed Regulator, but I really need to do some studying of regexes.
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Need help with some string manip in C#
« Reply #9 on: June 22, 2014, 03:35 AM »
RegexHero is another you might want to try.  Regexbuddy is the best Regex editor that I've come across.

Original address: https://www.donationcoder.com/forum/index.php?topic=38212.0