Solved

To remove white-space and leading zeros from string

Posted on 2016-10-06
12
74 Views
Last Modified: 2016-10-06
Hi guys,
  I working on input validation. I need to remove all white-space and leading zeros.

 // This is what I have:
string txtId = Web.GetData("IdValue").ToString();

 // removes white-space from string
txtId = Regex.Replace(txtId , @"\s", ""); 

 // How to remove leading zeros as well?

Open in new window



Thank you!
0
Comment
Question by:Darius
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +2
12 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 41831549
Hi,m

pls try

txtId = Regex.Replace(txtId , @"0*([1-9][0-9]*|0)", "$1"); 

Open in new window

Regards
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41831567
How about String.Trim like this.
txtId = txtId.Trim(new Char[] {' ', '0'});

Open in new window

1
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41831630
is your value numeric? the simplest way to remove the leading 0 would be to convert the value to a numeric datatype using the TryParse method: https://msdn.microsoft.com/en-us/library/system.int32.tryparse(v=vs.110).aspx
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41831679
I prefer either Fernando's or Eric's approaches, but if you're going to use Regex, then I'd suggest:

string result = Regex.Replace(original, "^0+|\s", string.Empy);

Open in new window

0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
ID: 41831682
Hi  Darius;

My first post will not work for you, this one will.
txtId = txtId.TrimStart(new Char[] {' ', '0'}).TrimEnd(new Char[] {' '});

Open in new window

1
 

Author Comment

by:Darius
ID: 41831688
Sorry guys, probably I have explained incorrectly.
I need to remove all white-space and trailing zeros from string in one function.

// input:                      txtId = "  000 0 0 ab - 123cd ef 12300  ";
// expected output:  txtId = "ab-123cdef12300";

txtId = Regex.Replace(txtId , @"0*([1-9][0-9]*|0)", "$1"); 
// output:  "  000 0 0 ab - 123cd ef 12300  "

txtId = txtId.Trim(new Char[] {' ', '0'});
// output:  "ab - 123cd ef 12300"

// investigate code myself
txtId = Regex.Replace(txtId, @"0*[\s]", "");
// output:  "ab-123cdef12300"

Open in new window


Thank you for help!
0
 

Author Comment

by:Darius
ID: 41831722
Hi käµfm³d 👽,
This is not works for me:
// input:    "  000 0 0 ab - 123cd ef 12300  "
string result = Regex.Replace(original, "^0+|\s", string.Empy);       //original
string result = Regex.Replace(original, @"^0+|\s", string.Empty);  //updated
// results:  "00000ab-123cdef12300"

Open in new window


Like I mentioned this works:
string result = Regex.Replace(original, @"0*[\s]", "");

Open in new window


Thanks again!
0
 

Author Comment

by:Darius
ID: 41831737
Sorry guys my mistake apologize!

Does not works my one as well!
0
 

Author Comment

by:Darius
ID: 41831739
testing now!!!
0
 

Author Comment

by:Darius
ID: 41831770
// My code incorrectly and removes  trailing and leading zeros.
results = Regex.Replace(txtId, @"0*[\s]", "");

Open in new window


// This works, but does it the proper way to do. Any other suggestion?
results = Regex.Replace(txtId, @"[\s]", "").TrimStart(new Char[] { ' ', '0' }).TrimEnd(new Char[] { ' ' });

Open in new window


Thanks
0
 

Author Comment

by:Darius
ID: 41831791
Probably I no need TrimEnd() function as I used Regex replace for white-space...

results = Regex.Replace(txtId, @"[\s]", "").TrimStart(new Char[] { ' ', '0' });

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 41831836
It was not clear from your original statement that "leading zeroes" meant a mix of leading zeroes and whitespaces. Corrected:

http://ideone.com/Ab0KPK

^[\s0]+|\s

Open in new window

1

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question