?
Solved

To remove white-space and leading zeros from string

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

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

800 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