String without 0

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Mast
Posts: 65
Joined: Wed Aug 29, 2007 6:24 am
Location: France

String without 0

Post by Mast » Fri Sep 28, 2007 10:21 pm

I search a example to delete all "0" on the left of a string. Example :

toto as string

toto = "00057.4"
for
toto = "57.4"

this string has not the same number of "0"

thanks

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sat Sep 29, 2007 7:21 pm

This should left justify the string (untested):

Code: Select all

Sub LeftJustifyStr(ByRef pstr As String)
   FSR0 = @pstr
   FSR1 = @pstr
   While POSTINC0 = 48         // "0"
   Wend
   Dec(FSR0)
   Repeat
      POSTINC1 = POSTINC0
   Until INDF0 = 0
   INDF1 = 0
End Sub

Mast
Posts: 65
Joined: Wed Aug 29, 2007 6:24 am
Location: France

Post by Mast » Sun Sep 30, 2007 6:49 am

Thank you xor

User avatar
RadioT
Registered User
Registered User
Posts: 157
Joined: Tue Nov 27, 2007 12:50 pm
Location: Winnipeg, Canada

Post by RadioT » Sun May 12, 2013 12:18 am

In a similar way, I was being driven mad trying to figure out why a number in a string being converted to a decimal using StrToDec kept on getting a wrong answer. It kept coming up with the likes of decimal "253" as a value when I knew darn well the original string had only an ascii "0" (hex 30).

It turned out there was a carriage return after the number!! So I made a function to take out the offending carriage return (hex 0D, or decimal 13). I made a variation of the function above to do it.

This function reads the input string into pItem and puts out the result with pStr. Since it's a function, the contents of pStr is returned. If you want a different character, substitute the "13" for a decimal value of whatever character you want to stop at. The function always reads from left to right in your string.

Code: Select all

Function RemoveCRFromEnd0fString(ByRef pStr As String, ByRef pItem As String) as boolean
   result = False
   FSR0 = @pStr
   FSR1 = @pItem
   Repeat
      POSTINC1 = POSTINC0
   Until INDF0 = 13
   INDF1 = 0
End Function
You call it like this:

Code: Select all

RemoveCRFromEnd0fString(original_string, output_corrected_string)
[/code]

Post Reply