11 Replies - 12714 Views - Last Post: 10 April 2007 - 11:17 AM Rate Topic: -----

#1 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Choosing what character you want to trim

Posted 10 April 2007 - 07:16 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Choosing what character you want to trim

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Choosing what character you want to trim

Posted 10 April 2007 - 07:17 AM

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.
Was This Post Helpful? 0
  • +
  • -

#3 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Choosing what character you want to trim

Posted 10 April 2007 - 07:36 AM

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.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Choosing what character you want to trim

Posted 10 April 2007 - 07:44 AM

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.
Was This Post Helpful? 0
  • +
  • -

#5 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Choosing what character you want to trim

Posted 10 April 2007 - 07:53 AM

VB.NET

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

Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Choosing what character you want to trim

Posted 10 April 2007 - 08:15 AM

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.
Was This Post Helpful? 0
  • +
  • -

#7 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Choosing what character you want to trim

Posted 10 April 2007 - 08:38 AM

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)
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Choosing what character you want to trim

Posted 10 April 2007 - 08:58 AM

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.
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Choosing what character you want to trim

Posted 10 April 2007 - 10:07 AM

View Postfyrestorm, on 10 Apr, 2007 - 07:53 AM, said:

VB.NET

And out of curiosity, can I do this:
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.
string = string.TrimStart("0")


If the string was "000123456789" then the value returned and stored in string will be "123456789".
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Choosing what character you want to trim

Posted 10 April 2007 - 10:22 AM

Excellent...thanks jayman!
Was This Post Helpful? 0
  • +
  • -

#11 mmcdanie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-April 07

Re: Choosing what character you want to trim

Posted 10 April 2007 - 10:32 AM

Why not just complicate this with a regular expression!

Regex re = new Regex("^0+", RegexOptions.Singleline);
string trimmed = re.Replace("000001121212", "");
Was This Post Helpful? 0
  • +
  • -

#12 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Choosing what character you want to trim

Posted 10 April 2007 - 11:17 AM

View Postjayman9, on 10 Apr, 2007 - 09:07 AM, said:

View Postfyrestorm, on 10 Apr, 2007 - 07:53 AM, said:

VB.NET

And out of curiosity, can I do this:
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.
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 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1