B4A Library String Functions

Discussion in 'Additional libraries, classes and official updates' started by margret, Jul 22, 2011.

  1. ivavilagu

    ivavilagu Member Licensed User

    You are right. I try with 1.04 and doesn´t work but with the 1.03 all work fine without errors.

    Thanks!!!!!
     
  2. thehe

    thehe Member Licensed User

    I have three images: a.png, b.png, c.png
    In Sub Globals

    Dim bitNameAr(3) As String
    Private ImageView1 As ImageView
    Private Button1 As Button

    In Activity-Create:
    bitNameAr(0) = "a.png"
    bitNameAr(1) = "b"
    bitNameAr(2) = ".png"

    Sub Button1_Click
    ' ImageView1.Bitmap = LoadBitmap(File.DirAssets, bitNameAr(0)) ' Work with bitNameAr(0) = "a.png"
    ' ImageView1.Bitmap = LoadBitmap(File.DirAssets, bitNameAr(1) + ".png" )' do NOT work with bitNameAr(1) = "b"
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, "c" + bitNameAr(2) )' do NOT work with bitNameAr(2) = ".png"
    End Sub
    Would please help Between String Array and String
    I expect I can ADD before or AFTER Array of String a,b, c to simplify declaration Array(0) + ".mp3" or Array(0) + ".png" . . . .
    Van
     
  3. Informatix

    Informatix Expert Licensed User

    The operator to concatenate strings is &, not +.
     
  4. DaveW

    DaveW Active Member Licensed User

    Hi Margret,
    I tried the SplitGetWord() function but it only seems to return the first letter of the element, not the whole element.
    i.e. ANS = SF.SplitGetWord("This|is|a|test|string.", "|", 2) returns "i" not "is".
     
  5. Chr6373

    Chr6373 New Member Licensed User

    Tutorial says:
    InString
    (YourString As String, SearchFor As String))
    Returns the position of the SearchFor within YourString."


    Like the SplitGetWord() function, note a result that can occur when using a quoted search using InString.
    This is important to know when working with "CSV1","CSV2" types of data strings.


    Example in B4A:
    Dim strTest As String
    strTest = "Test" & Chr("34") & "This"
    Dim intTest As Int
    intTest = sf.InString(strTest,"Th")
    Msgbox("A test using the phrase: " & strTest & CRLF & "intTest = sf.instring(strTest," & Chr(34) & "Th" & Chr(34) & ") = " & "intTest=" & intTest,"Example")

    MSGBOX RESULTS:

    A test using the phrase: Test"This
    intTest = sf.InString(strTest,"Th") = intTest=5

    While B4A reports intTest=5, in Visual Basic this would be intTest=6







     
  6. susu

    susu Well-Known Member Licensed User

    Split seems not work with delimiter "|"

    sf.Split("This is first|This is second|This is third", "|")

    Result will be:
    "T"
    "h"
    "i"
    "s"
    .....
     
  7. LucaMs

    LucaMs Expert Licensed User

    sf.Split("This is first|This is second|This is third", "\|")
     
  8. shashkiranr

    shashkiranr Active Member Licensed User

    Hi All,

    Getting the below exception for MidExtract
    Code:
    Bank:kjjhhg:Cardnumber/355478/cvv;39588;


    java.lang.StringIndexOutOfBoundsException: length=
    0; regionStart=1; regionLength=-1


        at java.lang.String.startEndAndLength(
    String.java:588)
        at java.lang.String.substring(
    String.java:1475)
        at adr.stringfunctions.stringfunctions._vvvv6(
    stringfunctions.java:479)
        at com.bayalu.rahasya.encryption._decrypt_data(encryption.java:
    321)
        at com.bayalu.rahasya.encryption._encrypt_data(encryption.java:
    523)
        at com.bayalu.rahasya.adddata._savebutton_click(adddata.java:
    602)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    187)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    175)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    171)
        at anywheresoftware.b4a.objects.ViewWrapper$
    1.onClick(ViewWrapper.java:77)
        at android.view.View.performClick(
    View.java:4654)
        at android.view.View$PerformClick.run(
    View.java:19438)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    146)
        at android.app.ActivityThread.main(ActivityThread.java:
    5602)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    1099)
        at dalvik.system.NativeStart.main(Native Method)

    The following code is used

    Code:
    fd = Bank:kjjhhg:Cardnumber/355478/cvv;39588

    sf.Initialize
    Dim res As String = sf.MidExtract(fd,":",":")
    Regards,
    SK
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    I'm not familiar with this library implementation however you can use Regex:
    Code:
    Dim s As String = "Bank:kjjhhg:Cardnumber/355478/cvv;39588"
    Dim m As Matcher = Regex.Matcher(":([^:]+):", s) 'the pattern will look for a colon followed by any number of characters other than colon and then a color. The characters in the middle are grouped.
    If m.Find Then
       
    Log(m.Group(1))
    End If
     
    shashkiranr likes this.
  10. Troberg

    Troberg Well-Known Member Licensed User

    Suggestion:

    Two string handling functions I've made which I use a a lot and find very handy are these:

    'Count the number of values in a string, separated by Separator
    CountValues(Text as string, Separator as string) as int
    'Returns the value at ValueNumber Position, using Separator to count the values. Returns empty string if no value.
    GetValueNumber(Text as string, Separator as string, ValueNumber as string) as string

    I know this can be done by splitting the string into an array, but that's more awkward for a simple case. For example:

    'Splitting a key-value string from a line read from a file
    Key=GetValueNumber(Line, "=", 1)
    Value=GetValueNumber(Line, "=", 2)

    Neat and gives clear, readable code. I think these are the second most used methods I use, counted by how many project they are included in.
     
    shashkiranr likes this.
  11. Troberg

    Troberg Well-Known Member Licensed User

    Some other handy string functions I use a lot (all of these can be done with a few lines, but I find the code much more readable like this):

    BeginsWith(Text as string, FindStr as string) as bool
    'Returns true if Text begins with FindStr
    EndsWith(Text as string, FindStr as string) as bool
    'Returns true if Text ends with FindStr, very useful when checking file extensions
    Contains(Text as string, FindStr as string) as bool
    'Returns true if Text contains FindStr
    CutFirst(Text as string, CutLen as int) as string
    'Cuts the first CutLen characters from Text
    CutLast(Text as string, CutLen as int) as string
    'Cuts the last CutLen characters from Text
    FilterText(Text as string, Allowed as string) as string
    'Removes all characters from Text which are not in Allowed
    GetBetween(Text as string, StartMarker as string, EndMarker as string, StartPos as int) as string
    'Gets the content between StartMarker and EndMarker in Text, starting the search at StartPos. Useful for situations like getting the number from "Somefile (3).ext"

    Some may think it's excessive to make simple 1-3 line subs like these, but they make for much more readable code. I've made a program to format e-books in txt format, and it makes heavy use of string handling, and these has made the code approximately 3800 times more readable.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Most of these methods are already included in the Strings object:
    Code:
    "abc".StartsWith / EndsWith / Contains / SubString
     
    Troberg likes this.
  13. DonManfred

    DonManfred Expert Licensed User

    Some of them alredy there (in the core!)
    Code:
    Dim test As String
        
    Log("Contains e:"&test.Contains("E"))
        
    Log("EndsWith a:"&test.EndsWith("a"))
        
    Log("StartWith T:"&test.StartsWith("T"))
    just to name some... Did not look deeper
     
    Troberg likes this.
  14. Troberg

    Troberg Well-Known Member Licensed User

    Sorry, I'm not at my B4A computer, so I couldn't check, so I just went with what I remembered that I had written for my TextFormat program.
     
  15. henrywood

    henrywood Active Member Licensed User

    @margret: Could you be persuaded to upload the B4A code (.bas file) for StringFunctions 1.05, please ?

    I would like to use it in my iOS / B4i app where your excellent library is sadly missing....

    Thanks


    /Henrik
     
  16. Johan Schoeman

    Johan Schoeman Expert Licensed User

    I am not familiar with B4I but seeing that it supports inline Objective C code you could probably do something similar as to what I am trying to demonstrate in the attached B4A project with inline Java code. The Web is full of all kinds of methods that one can manipulate strings with (Java and C) for eg StackOverflow has plenty of examples. I guess B4I will also allow you to compile the newly created class to a library too. Thus, you should be able to add string manipulation methods to your liking and compile it to a library...

    The B4A code in the class only takes care of the first four Java methods in the class. Add the additional B4A code in the class to call the other Java methods in the class. And then add to it as much as you want (and compile it to a library)
     

    Attached Files:

    Last edited: Apr 16, 2015
    Mashiane likes this.
  17. henrywood

    henrywood Active Member Licensed User

    thanks I'll have a look
     
  18. appie21

    appie21 Active Member Licensed User

    Hello

    I have a string

    a = "Hello how are you today"

    How can i get tris strinfg to get start at 7 so that i get "how are you today"

    I see the Mid but how do it works

    i have install the library
     
  19. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    Use substring.
    Code:
    Dim s as string
    s = 
    "Hello how are you today"
    Log (s.SubString(6))
     
  20. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Mid works as follows:

    Dim sf as StringFunctions
    Dim a as String
    a = "Hello how are you today"
    sf.Initialize
    a = sf.Mid(a, 7, 17) 'The string to use, the starting char number counting from 1, the number of characters to extract from and including the starting character.
    Log(a)

    You could also do:
    a = sf.Mid(a, 7, sf.Len(a) - 6)

    The above only to explain the Mid function of this library. Not sure I have all the CAPS correct - typing this on my IPad
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice