Solved

To remove white-space and leading zeros from string

Posted on 2016-10-06
12
41 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 48

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 69

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

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 74

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now