Solved

To remove white-space and leading zeros from string

Posted on 2016-10-06
12
50 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
  • 6
  • 2
  • 2
  • +2
12 Comments
 
LVL 49

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 62

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 62

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

770 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