Android Question Trimming a string after X spaces

Sub7

Active Member
Licensed User
Longtime User
Hello,
how can i trim a string afer X number of spaces, for example:

Input: I love Basic4android community
Output: I love Basic4android

String lenght is not fixed so i cannot count and trim.

Ty
 

stevel05

Expert
Licensed User
Longtime User
A sub similar to this will do it:

B4X:
'Returns NoFields delimited fields from record starting from StartField (first = 0)

Sub GetFields(Record As String,FieldDelim As String,StartField As Int,NoFields As Int) As String
    Dim Count,SkipCount,ThisStart,StartPos As Int
    Dim LastErrorMessage A string

    'Determine the start position of the 1st required field
    SkipCount=0
    StartPos=0
    Do While SkipCount < StartField
        StartPos=Record.IndexOf2(FieldDelim,StartPos)+1
        If StartPos=0 Then Exit
        SkipCount=SkipCount+1
    Loop
    If SkipCount > 0 AND StartPos = 0 Then
        LastErrorMessage="Error: StrUtilsLib GetFields: StartPos is past the end of the record"
        Log(LastErrorMessage)
        Return -1
    End If

    Count=0
    ThisStart=StartPos
    Do While Count < NoFields
        ThisStart=Record.IndexOf2(FieldDelim,ThisStart)+1
        If ThisStart=0 Then Exit
        Count=Count+1
    Loop
    If ThisStart=0 Then
        'No Fields Found
        If Count = 0 AND NoFields > 1 AND SkipCount = 0 Then
            LastErrorMessage="Error: StrUtilsLib GetFields: "&FieldDelim&" not found"
            Log(LastErrorMessage)
            Return -1
        End If
        'Last Delimeter is missing, return whole string
        LastErrorMessage="Warning: StrUtilsLib GetFields: Missing last FieldDelim(s) '"&FieldDelim&"' Record returned from StartPos"
        Log(LastErrorMessage)
        Return Record.SubString(StartPos)
    Else
        Return Record.SubString2(StartPos,ThisStart)
    End If
End Sub

Call it as:

B4X:
Result = GetFields(string," ",0,3)

It counts the number of words to be returned rather than the number of spaces. It's quite large but it's general purpose.
 
Last edited:
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
B4X:
Dim sf as stringfunction
Dim str as String="Your String"
Dim reslt as string
For i=0 To str.Length-1
        If str.Contains(" ") Then
            reslt=sf.Stuff(str,sf.addspace(0)," ")
        End If
Next
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You can use Regex too:
B4X:
Dim var As String
Dim pattern As String
Dim Matcher1 As Matcher

var = "    some                                  spaces and      text"

pattern = "[ ]{2,}"

Matcher1 = Regex.Matcher(pattern, var)

Do While Matcher1.Find

    var = var.Replace(Matcher1.Match, " ")

Loop 

Msgbox(var, "")
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
You can use Regex too:
B4X:
Dim var As String
Dim pattern As String
Dim Matcher1 As Matcher

var = "    some                                  spaces and      text"

pattern = "[ ]{2,}"

Matcher1 = Regex.Matcher(pattern, var)

Do While Matcher1.Find

    var = var.Replace(Matcher1.Match, " ")

Loop

Msgbox(var, "")
Hi Njdude, this not seems to works to me.
B4X:
Dim var As String
Dim pattern As String
Dim Matcher1 As Matcher
var = "I love basic4 android"
pattern = "[ ]{2,}"

Matcher1 = Regex.Matcher(pattern, var)

Do While Matcher1.Find
var = var.Replace(Matcher1.Match, " ")
Loop
Msgbox(var, "")

Result should be: I love

ty
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You can use the code I posted to "pre-process" the string, after processing you will end up with 1 space between words, from there, you could use SubString or any other method to do what you need.
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
OK guys, thats fine!

But how to do for this example:
I have a table in SQL with music artists and their songs.
1) Sometimes the artistst are written like this: "THE SUGARHILL GANG" ... and ...
2) Sometimes the artistst are written like this: "THE SUGARHILL GANG "

As you can see, there is a space behind the example 2.
How can I delete the spaces (only the spaces behind the names, not the spaces between the words) of ALL artists in the table by "one click"?

A simple solution is ...
SQL1.ExecNonQuery("UPDATE Daten001 SET Feld001 = 'THE SUGARHILL GANG' WHERE Feld001 = 'THE SUGARHILL GANG ' ")
SQL1.ExecNonQuery("UPDATE Daten001 SET Feld001 = 'MICHAEL JACKSON' WHERE Feld001 = 'MICHAEL JACKSON ' ")
... but thats not a real good solution if you have hundreds of artists!
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You can use the RTRIM keyword. Please make a copy of the database before doing this as I have not had a chance to test it today:
B4X:
SQL1.BeginTransaction
SQL1.ExecNonQuery("UPDATE Daten01 SET Feld001 = RTRIM(Feld001)")
SQL1.TransactionSuccessful
SQL1.EndTransaction

Edit: Corrected syntax 9:24 AM
 
Last edited:
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Thank you Mahares! It seems to work with your code. GREAT!

There was only one error message because of a syntax error ...
'SQL1.ExecNonQuery("UPDATE Daten01 SET Feld001 = RTRIM(Feld001") 'original has syntax error

It works with this code:
B4X:
SQL1.ExecNonQuery("UPDATE Daten001 SET Feld001 = RTRIM(Feld001)")
 
Upvote 0
Top