topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Wednesday December 11, 2024, 11:47 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • View Profile
    • Donate to Member
Need help with some string manip in C#
« on: June 21, 2014, 05: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."

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
    • View Profile
    • Donate to Member
Re: Need help with some string manip in C#
« Reply #1 on: June 21, 2014, 06: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
    • View Profile
    • Donate to Member
Re: Need help with some string manip in C#
« Reply #2 on: June 21, 2014, 06: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"

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: Need help with some string manip in C#
« Reply #3 on: June 21, 2014, 07:06 PM »
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
    • View Profile
    • Donate to Member
Re: Need help with some string manip in C#
« Reply #4 on: June 21, 2014, 07:13 PM »
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"

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
    • View Profile
    • Donate to Member
Re: Need help with some string manip in C#
« Reply #5 on: June 21, 2014, 08:09 PM »
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,914
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Need help with some string manip in C#
« Reply #6 on: June 21, 2014, 09:19 PM »
I think a regex is the way to go.  Easy to make one that ignores whitespaces, etc.

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: Need help with some string manip in C#
« Reply #7 on: June 21, 2014, 09:55 PM »
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
    • View Profile
    • Donate to Member
Re: Need help with some string manip in C#
« Reply #8 on: June 21, 2014, 10:02 PM »
Yeah, I installed Regulator, but I really need to do some studying of regexes.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,190
    • View Profile
    • Donate to Member
Re: Need help with some string manip in C#
« Reply #9 on: June 21, 2014, 10:35 PM »
RegexHero is another you might want to try.  Regexbuddy is the best Regex editor that I've come across.