Trimming characters from a string
BlitzMax Forums/BlitzMax Beginners Area/Trimming characters from a string| 
 | ||
| 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 | 
| 
 | ||
| Local s_time:String = CurrentTime() If s_time[0..1] = "0" Then s_time = s_time[1..5] | 
| 
 | ||
| Lovely, thank you! | 
| 
 | ||
| @Dave, I liked your first one more. (No offence Pert), it was a lot easyer to understand | 
| 
 | ||
| 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 :þ ) | 
| 
 | ||
| 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..]
 | 
| 
 | ||
| even smaller: 'Then' can be ommited .. | 
| 
 | ||
| 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) | 
| 
 | ||
| Even slightly faster.. No Asc() conversion required... Local s_time:String = CurrentTime() If s_time[0] = 48 s_time = s_time[1..] ;-) | 
| 
 | ||
| Variant: Local s_time:String = CurrentTime() s_time = Int(s_time)+s_time[3..] | 
| 
 | ||
|  Even slightly faster.. No Asc() conversion required...  You do know that Asc(StringConstant) is handled at compile time right? ;) |