B4J Library 90+ String Manipulation Methods

This is a partial wrap of the Apache StringUtils library. Which is part of the Apache Commons components. Since there is already a StringUtils library in B4J I have called it ApacheSU and the library JAR is ApacheStringUtilsB4J.


ApacheStringUtilsB4J
Author:
Wrapped By KeirS
Version: 1.01
  • ApacheSU
    Methods:
    • AppendIfMissing (str As String, suffix As String, suffixes() As String) As String
      Abbreviates a String to the length passed, replacing the middle characters with the supplied replacement String.
    • Capitalize (str As String) As String
      Capitalizes a String changing the first character to title case.
    • Center (str As String, size As Int) As String
      Centers a String in a larger String of size size using the space character.
    • CenterPad (str As String, size As Int, padStr As String) As String
      Centers a String in a larger String of size size.
    • Chomp (str As String) As String
      Removes one newline from end of a String if it's there, otherwise leave it alone.
    • Chop (str As String) As String
      Remove the last character from a String.
    • ContainsAny (stringToSearch As String, searchFor As String) As Boolean
      Checks if the String contains any character in the given set of characters.
    • ContainsNone (stringToSearch As String, invalidChars As String) As Boolean
      Checks that the String does not contain certain characters.
    • ContainsOnly (stringToSearch As String, validChars As String) As Boolean
      Checks if the String contains only certain characters.
    • ContainsSeq (seq As String, searchSeq As String) As Boolean
      Checks if String contains a search String, handling null.
    • ContainsWhitespace (seq As String) As Boolean
      Check whether the given String contains any whitespace characters.
    • CountMatches (str As String, sub As String) As Int
      Counts how many times the substring appears in the larger string.
    • DeleteWhitespace (str As String) As String
      Deletes all whitespaces from a String.
    • Difference (str1 As String, str2 As String) As String
      Compares two Strings, and returns the portion where they differ.
    • EndsWith (str As String, suffix As String) As Boolean
      Check if a String ends with a specified suffix.
    • EndsWithIgnoreCase (str As String, suffix As String) As Boolean
      Case insensitive check if a String ends with a specified suffix.
    • IndexOf (seq As String, searchChar As Int, startPos As Int) As Int
      Finds the first index within a String from a start position, handling null.
    • IndexOfAny (cs As String, searchChars As String) As Int
      Search a String to find the first index of any character in the given set of characters.
    • IndexOfAnyBut (seq As String, searchChars As String) As Int
      Search a String to find the first index of any character not in the given set of characters.
    • IsAllLowerCase (Str As String) As Boolean
      Checks if the String contains only lowercase characters.
    • IsAllUpperCase (Str As String) As Boolean
      Checks if the String contains only uppercase characters.
    • IsAlpha (Str As String) As Boolean
      Checks if the String contains only Unicode letters.
    • IsAlphanumeric (Str As String) As Boolean
      Checks if the String contains only Unicode letters or digits.
    • IsAlphanumericSpace (Str As String) As Boolean
      Checks if the String contains only Unicode letters, digits or space (' ').
    • IsBlank (Str As String) As Boolean
      Checks if a String is whitespace, empty ("") or null.
    • IsEmpty (Str As String) As Boolean
      Checks if a String is empty ("") or null.
    • IsNotBlank (Str As String) As Boolean
      Checks if a String is not empty (""), not null and not whitespace only.
    • IsNotEmpty (Str As String) As Boolean
      Checks if a String is not empty ("") and not null.
    • IsNumeriSpace (Str As String) As Boolean
      Checks if the String contains only Unicode digits or space (' ').
    • IsNumeric (Str As String) As Boolean
      Checks if the String contains only Unicode digits.
    • IsWhitespace (Str As String) As Boolean
    • JoinChar (array() As Char, separator As Char) As String
      Checks if the String contains only whitespace.
    • JoinDouble (array() As Double, separator As Char) As String
      Joins the elements of the provided array into a single String containing the provided list of elements.
    • JoinFloat (array() As Float, separator As Char) As String
      Joins the elements of the provided array into a single String containing the provided list of elements.
    • JoinInt (array() As Int, separator As Char) As String
      Joins the elements of the provided array into a single String containing the provided list of elements.
    • JoinLong (array() As Long, separator As Char) As String
      Joins the elements of the provided array into a single String containing the provided list of elements.
    • JoinObject (array() As Object, separator As Char) As String
      Joins the elements of the provided array into a single String containing the provided list of elements.
    • LastIndexOf (seq As String, searchSeq As String) As Int
      Finds the last index within a String, handling null.
    • LastIndexOfIgnoreCaseStartAt (str As String, searchStr As String, startPos As Int) As Int
      Case in-sensitive find of the last index within a String from the specified position.
    • LastIndexOfStartAt (seq As String, searchSeq As String, startPos As Int) As Int
      Finds the last index within a String from a start position, handling null.
    • Left (str As String, len As Int) As String
      Gets the leftmost len characters of a String.
    • LeftPad (str As String, size As Int, padStr As String) As String
      Left pad a String with a specified String.
    • Length (Str As String) As Int
      Gets a String length or 0 if the String is null.
    • LowerCase (str As String) As String
      Converts a String to lower case
    • Mid (str As String, pos As Int, len As Int) As String
      Gets len characters from the middle of a String.
    • NormalizeSpace (str As String) As String
      Similar to http://www.w3.org/TR/xpath/#function-normalize -space The function returns the argument string with whitespace normalized by using trim(String) to remove leading and trailing whitespace and then replacing sequences of whitespace characters by a single space.
    • OrdinalIndexOf (str As String, searchStr As String, ordinal As Int) As Int
      Finds the n-th index within a String, handling null
    • Overlay (str As String, overlay As String, start As Int, end As Int) As String
      Overlays part of a String with another String.
    • Remove (str As String, remove As String) As String
      Removes all occurrences of a substring from within the source string.
    • RemoveAll (text As String, regex As String) As String
      Removes each substring of the text String that matches the given regular expression.
    • RemoveEnd (str As String, remove As String) As String
      Removes a substring only if it is at the end of a source string, otherwise returns the source string.
    • RemoveFirst (text As String, regex As String) As String
      Removes the first substring of the text string that matches the given regular expression.
    • RemoveIgnoreCase (str As String, remove As String) As String
      Case insensitive removal of all occurrences of a substring from within the source string.
    • RemovePattern (source As String, regex As String) As String
      Removes each substring of the source String that matches the given regular expression using the DOTALL option.
    • RemoveStart (str As String, remove As String) As String
      Removes a substring only if it is at the beginning of a source string, otherwise returns the source string.
    • RemoveStartIgnoreCase (str As String, remove As String) As String
      Case insensitive removal of a substring if it is at the beginning of a source string, otherwise returns the source string.
    • Repeat (str As String, repeat As Int) As String
      Repeat a String repeat times to form a new String.
    • RepeatWithSeparator (str As String, separator As String, repeat As Int) As String
      Repeat a String repeat times to form a new String, with a String separator injected each time.
    • Replace (text As String, searchString As String, replacement As String) As String
      Replaces all occurrences of a String within another String.
    • ReplaceAll (text As String, regex As String, replacement As String) As String
      Replaces each substring of the text String that matches the given regular expression with the given replacement.
    • ReplaceChars (str As String, searchChars As String, replaceChars As String) As String
      Replaces multiple characters in a String in one go.
    • ReplaceEach (text As String, searchList() As String, replacementList() As String) As String
      Replaces all occurrences of Strings within another String.
    • ReplaceEachRepeatedly (text As String, searchList() As String, replacementList() As String) As String
      Replaces all occurrences of Strings within another String.
    • ReplaceFirst (text As String, regex As String, replacement As String) As String
      Replaces the first substring of the text string that matches the given regular expression with the given replacement.
    • ReplaceIgnoreCase (text As String, searchString As String, replacement As String) As String
      Case insensitively replaces a String with another String inside a larger String, once.
    • ReplaceOnce (text As String, searchString As String, replacement As String) As String
      Replaces a String with another String inside a larger String, once.
    • ReplaceOnceIgnoreCase (text As String, searchString As String, replacement As String) As String
      Case insensitively replaces a String with another String inside a larger String, once.
    • ReplacePattern (source As String, regex As String, replacement As String) As String
      Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern.DOTALL option.
    • Reverse (str As String) As String
      Reverses a String
    • ReverseDelimited (str As String, separatorChar As Char) As String
      Reverses a String that is delimited by a specific character.
    • Right (str As String, len As Int) As String
      Gets the rightmost len characters of a String.
    • RightPad (str As String, size As Int, padStr As String) As String
      Right pad a String with a specified String.
    • Rrotate (str As String, shift As Int) As String
      Rotate (circular shift) a String of shift characters.
    • Split (str As String) As String[]
      Splits the provided text into an array, using whitespace as the separator.
    • SplitWithSeparator (str As String, separatorChars As String) As String[]
      Splits the provided text into an array, separator specified.
    • StartsWith (str As String, prefix As String) As Boolean
      Check if a String starts with a specified prefix.
    • StartsWithIgnoreCase (str As String, prefix As String) As Boolean
      Case insensitive check if a String starts with a specified prefix.
    • Strip (str As String, stripChars As String) As String
      Strips any of a set of characters from the start and end of a String.
    • StripEnd (str As String, stripChars As String) As String
      Strips any of a set of characters from the end of a String.
    • StripStart (str As String, stripChars As String) As String
      Strips any of a set of characters from the start of a String.
    • Substring (str As String, start As Int, end As Int) As String
      Gets a substring from the specified String avoiding exceptions.
    • SubstringAfter (str As String, separator As String) As String
      Gets the substring after the first occurrence of a separator.
    • SubstringAfterLast (str As String, separator As String) As String
      Gets the substring after the last occurrence of a separator.
    • SubstringBefore (str As String, separator As String) As String
      Gets the substring before the first occurrence of a separator.
    • SubstringBeforeLast (str As String, separator As String) As String
      Gets the substring before the last occurrence of a separator.
    • SubstringBetween (str As String, tag As String) As String
      Gets the String that is nested in between two instances of the same String.
    • SubstringBetween2 (str As String, open As String, close As String) As String
      Gets the String that is nested in between two Strings.
    • SubstringsBetween (str As String, open As String, close As String) As String[]
      Searches a String for substrings delimited by a start and end tag, returning all matching substrings in an array.
    • SwapCase (str As String) As String
      Swaps the case of a Dtring.
    • Trim (str As String) As String
      Removes control characters (char <= 32) from both ends of this String, handling null by returning null.
    • Truncate (str As String, offset As Int, maxWidth As Int) As String
      Truncates a String.
    • Uncapitalize (str As String) As String
      Uncapitalizes a String, changing the first character to lower case.
    • UpperCase (str As String) As String
      Converts a String to upper case .
    • Wrap (str As String, wrapWith As String) As String
      Wraps a String with another String.
    • WrapIfMissing (str As String, wrapWith As String) As String
      Wraps a string with a string if that string is missing from the start or end of the given string.

Usage:
B4X:
Dim APSU As ApacheSU
    Log(APSU.Reverse("ABCDEFGHIJ"))
    Log(APSU.ContainsNone("ABCDEFGHIJ","AZ"))
    Dim join As String = APSU.JoinInt(Array As Int (1,2,3,4,5),",")
    Log(join)

To install unzip the zip and copy the contents into your additional libraries folder. I haven't tested all the methods but they should all work.
 

Attachments

  • ApachStringUtilsB4J.zip
    150.5 KB · Views: 787
Last edited:
Top