Trimming characters from a string

BlitzMax Forums/BlitzMax Beginners Area/Trimming characters from a string

DavidDC(Posted 2007) [#1]
Simple question:

In my clock routine, I have the following code

'Chop off preceding 0's
If CurrentTime()[0..1].ToInt() = 0
s_time = CurrentTime()[1..5]
Else
s_time = CurrentTime()[0..5]
End If

CurrentTime() returns a time string, with a preceding '0' if the hour is less than 10 - i.e. say 08:50 am vs 11:15 pm

I just want to lop off that preceding 0 if it exists. The code above looks inelegant to me and I'm wondering how to best go about this?

Thanks for any help

David


Perturbatio(Posted 2007) [#2]
Local s_time:String = CurrentTime()
If s_time[0..1] = "0" Then s_time = s_time[1..5]



DavidDC(Posted 2007) [#3]
Lovely, thank you!


H&K(Posted 2007) [#4]
@Dave,
I liked your first one more. (No offence Pert), it was a lot easyer to understand


Perturbatio(Posted 2007) [#5]
Just to make it *very slightly* smaller:

Local s_time:String = CurrentTime()
If s_time[..1] = "0" Then s_time = s_time[1..]


@Dave,
I liked your first one more. (No offence Pert), it was a lot easyer to understand


No offence taken, you're entitled to your opinions (even if you're crazy :þ )


grable(Posted 2007) [#6]
This should be slightly faster. (if speed is your thing ;)

Local s_time:String = CurrentTime()
If s_time[0] = Asc("0") Then s_time = s_time[1..]



CS_TBL(Posted 2007) [#7]
even smaller: 'Then' can be ommited ..


H&K(Posted 2007) [#8]
Local S_time:String=CurrentTime()[CurrentTime()[..1]="0"..]
Though I still think that Dave's first one is easyer to understand. And this might be slower, cos I dont know how slow CurrentTime is. (And its doing two slices, which cannot be fast)


Brucey(Posted 2007) [#9]
Even slightly faster.. No Asc() conversion required...
Local s_time:String = CurrentTime()
If s_time[0] = 48 s_time = s_time[1..]


;-)


Beaker(Posted 2007) [#10]
Variant:
Local s_time:String = CurrentTime()
s_time = Int(s_time)+s_time[3..]



grable(Posted 2007) [#11]
Even slightly faster.. No Asc() conversion required...


You do know that Asc(StringConstant) is handled at compile time right? ;)