Android Question Trying to trim a String

WiLey2000

Member
Licensed User
Longtime User
Morning All

I'm trying to trim a variable string "sItem"

sItem = "1. Equipment has all Speeds\Motions"

This is what I'm looking to do

sItem = "1"

This string is selected from a ListView. All the Items in the
list begin with a number followed by a period. All I want is the number.

Here's what I have - It is returning the whole string
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Dim sItem As String
    lblDefItemNo.Text = Value
    sItem = lblDefItemNo.Text
    sDefItem = LTrimSpaces(sItem)
    ListView1.RemoveView

End Sub

Sub LTrimSpaces(s As String) As String

  Dim i As Int

  For i = 0 To s.Length - 1
    If s.CharAt(i) <> "." Then
    Return s.SubString(i)
   End If
  Next
End Sub

What am I doing wrong?

John
 

Lahksman

Active Member
Licensed User
Longtime User
substring gives you everything on the right of your text.

You should use substring2 instead of substring.
B4X:
Sub LTrimSpaces(s As String) As String

  Dim i As Int

  For i = 0 To s.Length - 1
    If s.CharAt(i) = "." Then
    Return s.SubString2(0,i)
   End If
  Next
End Sub
 
Upvote 0
Top