B4A Library New version of MLStr string library

This is a new version of my old MLStr strings library. it includes methods to find and retrieve whole words from a string as well as some methods that I use a lot in b4a and other languages. It is small and a bit specialized, but quite useful.

A test app is included in the zip. Unzip the file and copy MLStr.jar and MLStr.xml to your additional libraries directory. Unzip the mlstrtest.zip somewhere. Then refresh from the IDE libs tab (right click in that panel) if b4a is already running, or just start b4a and open the Strtst project.

Current version is 2.2.0

MLStr
Author:
Jem Miller
Version: 2.2
  • MLStr
    Fields:
    • Remainder As String
    Methods:
    • IsInitialized As Boolean
      Tests whether the object has been initialized.
    • AposCheck (var As String) As String
      Checks to see if the string "var" contains apostrophies. If so, it puts them in a set of single quotes
      This is primarily for SQL statements.
    • Copies (inp As String, num As Int) As String
      Returns a string of "num" occurances of "inp"

      copies("*",5) will return 5 *'s
    • Escape (Str As String) As String
      Escapes the spaces in a stirng.
      Primarily used for file paths like C:\Program Files (x86) or other paths with spaces

      tmp = str.Escape(tmp) if used with the path above will return:
      C:\Program\ Files\ (x86) which is linux (and android) friendly.
    • getWords (StartWord As Int, NumWords As Int, Str As String) As String
      This method ignores punctuation
      Punctuation is defined as any character not in a-z, A-Z, or 0-9
      all punctuation is counted as a word break like a space

      st = getWords(2,5,"(this is)a test_of my-lib") Returns "is a test of"
    • getwords2 (StartWord As Int, NumWords As Int, Str As String) As String
      Returns NumWords form Str beginning at StartWord
      Returns "" if StartWord > total words in Str or if Str = ""
      Word count begins at 1 and not 0.

      if NumWords > the number of words remaining in the line, the remainder of the line is returned from StartWord count
      In this method, only spaces are used as word breaks

      st = getwords2(2,5,"(this is)a test_of my-lib") Returns "is)a test_of my-lib"
    • hex (numb As Int) As String
      Returns the hex string value of a number
    • initialize As String
      Initializes the MLStr object.
    • isChar (ch As Char) As Boolean
      Checks to see if a character is a normal character (a-z, A-Z, or 0-9)
    • isPunct (ch As Char) As Boolean
      Checks to see if a character is punctuation (not a-z, A-Z, or 0-9)
    • LastPos (Find As Char, Str As String) As Int
      Returns the last position of "Find" in "Str"
    • NewLines (num As Byte) As String
      Returns "num" occurances of CRLF. This is useful for formatting text files

      newlines(3) returns 3 linefeeds
    • PosWord (wordNum As Int, Str As String) As Int
      Returns the character postion of the first letter of the Word at postion NumWord

      i = PosWord(4,"This is a test test of the MLStr-library") returns 11 (the first letter of the word test)
      Returns 0 if error
    • PosWord2 (wordNum As Int, Str As String) As Int
      Returns the character postion of the first letter of the Word at postion NumWord

      i = PosWord2(4,"This is a test test of the MLStr-library") returns 11 (the first letter of the word test)
      Returns 0 if error

      This method uses spaces as word breaks.
    • Proper (line As String) As String
      Uppercases the first letter of EACH word in a string
    • Strip (line As String) As String
      Strips ALL spaces from "line"
    • StripIt (line As String, ch As Char) As String
      Strips all occurances of the character "ch" from the string "line"
    • StripTo (line As String, find As String) As String
      Returns everything in the string "line" up to the point where it finds the string "find".
      Deletes everything before "find" and returns what is left in the Remainder string.
      Find can be a single character or a string.

      stripto("this is a string"," ") will return "this" and Remainder will equal "is a string"
      Notice that the " " is gone from the return value AND in Remainder. This is
      true of all find values sent to the routine. The find value is always deleted.
    • Unique As String
      Returns a unique identifier based on the current date and time converted to hex
    • wordcnt (Str As String) As Int
      Same as words
    • wordcnt2 (Str As String) As Int
      Same as words2
    • WordN (st As String, nm As Byte) As String
      Returns word number "nm" from string "St"

      wordn("this is a test",3) will return "a"
      This method accounts for non- letters and non-numbers
    • WordN2 (st As String, nm As Byte) As String
      Returns word number "nm" from string "St"

      wordn2("this is a test",3) will return "a"
      This method only only uses spaces to break words.
    • Words (st As String) As Int
      Returns the number of words in "St"

      words("this is a test") will return 4.
      This method accounts for punctuation including all non-letters or non-numbers
    • Words2 (st As String) As Int
      Returns the number of words in "St"

      words2("this is a test") will return 4.
      This method uses spaces as word breaks

Version 2.0 - Initial release

Version 2.1 - Adds the Escape method that I forgot in v2.0 and fixes a bug in the StripTo method that did not properly handle strings as the Find target.

Version 2.20 - Adds new methods for using only spaces as word breaks. I didn't realize how many times I need to use spaces as breaks and ignore punctuation such as hyphens.

---Jem
 

Attachments

  • MLStr-2.2.zip
    16.4 KB · Views: 548
Last edited:

Mahares

Expert
Licensed User
Longtime User
Hey HotShoe: You name Jem says all. You are a real GEM providing this valuable lib.
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Added version 2.1
 

HotShoe

Well-Known Member
Licensed User
Longtime User
New version 2.20 is attached to first post. I added new methods to use only spaces as word breaks. I found I use this a lot in my coding and when I made the methods punctuation aware, it broke many of my coded routines.

--- Jem
 

MolsonB

Member
Licensed User
Longtime User
Trying the Copies feature, getting js error. (java.lang.NullPointerException)

B4X:
Dim str As MLStr
Dim Broadcast As String
Broadcast = str.Copies("x", 5)
 

DonManfred

Expert
Licensed User
Longtime User
You forget to initialize the library!
B4X:
    Dim str As MLStr
    str.Initialize
    Dim Broadcast As String
    Broadcast = str.Copies("x", 5)
 

MolsonB

Member
Licensed User
Longtime User
Doh... I was using a different library for string functions and got away with not initializing it (by mistake). This librabry replaced it, but didn't like not being initialized. Thanks for the catch.
 
Top