Welcome to Dream.In.Code
Become an Expert!

Join 149,499 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,383 people online right now. Registration is fast and FREE... Join Now!




Choosing what character you want to trim

 
Reply to this topicStart new topic

Choosing what character you want to trim

fyrestorm
10 Apr, 2007 - 06:16 AM
Post #1

D.I.C Lover
Group Icon

Joined: 4 Apr, 2002
Posts: 3,103



Thanked: 2 times
Dream Kudos: 228
My Contributions
I have a string that has zero's for a filler, I'd like to trim them from my string.

Is there a function that will trim just those leading zero's?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 06:17 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Are you using ASP, or ASP.NET? Also, is there a possibility that there will be other zeros in the string that you need to keep? If so, there is no specific function in classic ASP to trim specific characters, but you can use the Replace function in conjunction with the InStr function to eliminate the leading zeroes. Another possibility is the use of one of the conversion functions if the string is comprised of only digits.
User is offlineProfile CardPM
+Quote Post

fyrestorm
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 06:36 AM
Post #3

D.I.C Lover
Group Icon

Joined: 4 Apr, 2002
Posts: 3,103



Thanked: 2 times
Dream Kudos: 228
My Contributions
I'm using ASP.NET and I just want to trim the leading zeros, if there are other zeros in the string, they need to remain.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 06:44 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Are you using the ASP.NET functionality only, or are you using any VB.NET/C# as well on the page? Sorry for the questions, but if using one of those languages, we can use the String object.
User is offlineProfile CardPM
+Quote Post

fyrestorm
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 06:53 AM
Post #5

D.I.C Lover
Group Icon

Joined: 4 Apr, 2002
Posts: 3,103



Thanked: 2 times
Dream Kudos: 228
My Contributions
VB.NET

And out of curiosity, can I do this:
CODE
string.TrimStart("0")

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 07:15 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Actually, i'm not sure...TrimStart is meant to remove that character from the beginning...not sure if it will do it multiple times. you may also have to convert the parameter to a char array before feeding it to the TrimStart method.
User is offlineProfile CardPM
+Quote Post

fyrestorm
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 07:38 AM
Post #7

D.I.C Lover
Group Icon

Joined: 4 Apr, 2002
Posts: 3,103



Thanked: 2 times
Dream Kudos: 228
My Contributions
ok, let me ask, in the event that my method doesn't work, what are your thoughts on how to proceed with trimming the leading zeros? (just for future reference)
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 07:58 AM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Well, the brute force way would be to traverse the string starting at the beginning - if the character at the specified index is a "0", remove (or replace with nothing), and break out of the loop the first time you hit a character is not a zero. another way to do it would be to use the Replace function to change all the zeroes to spaces, then use the LTrim function to get rid fo those spaces, then use the Replace function again to reconvert the remaining spaces to zeroes.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 09:07 AM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,303



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
QUOTE(fyrestorm @ 10 Apr, 2007 - 07:53 AM) *

VB.NET

And out of curiosity, can I do this:
CODE
string.TrimStart("0")



Absolutely, this will trim all leading zeros from your string. The only thing is TrimStart will return a string so you need to assign it like this.
CODE

string = string.TrimStart("0")


If the string was "000123456789" then the value returned and stored in string will be "123456789".
User is online!Profile CardPM
+Quote Post

Amadeus
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 09:22 AM
Post #10

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Excellent...thanks jayman!
User is offlineProfile CardPM
+Quote Post

mmcdanie
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 09:32 AM
Post #11

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 1


My Contributions
Why not just complicate this with a regular expression!

Regex re = new Regex("^0+", RegexOptions.Singleline);
string trimmed = re.Replace("000001121212", "");
User is offlineProfile CardPM
+Quote Post

fyrestorm
RE: Choosing What Character You Want To Trim
10 Apr, 2007 - 10:17 AM
Post #12

D.I.C Lover
Group Icon

Joined: 4 Apr, 2002
Posts: 3,103



Thanked: 2 times
Dream Kudos: 228
My Contributions
QUOTE(jayman9 @ 10 Apr, 2007 - 09:07 AM) *

QUOTE(fyrestorm @ 10 Apr, 2007 - 07:53 AM) *

VB.NET

And out of curiosity, can I do this:
CODE
string.TrimStart("0")



Absolutely, this will trim all leading zeros from your string. The only thing is TrimStart will return a string so you need to assign it like this.
CODE

string = string.TrimStart("0")


If the string was "000123456789" then the value returned and stored in string will be "123456789".


Thanks jayman, that's what I did smile.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month