Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

To remove white-space and leading zeros from string

Posted on 2016-10-06
12
Medium Priority
?
113 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 53

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 64

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 800 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 1200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Suggested Courses

610 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