java.lang.Object
io.github.astrapi69.string.StringExtensions
The class
Note: As the
StringExtensions
provides methods for manipulate string objects.Note: As the
String
class is immutable not the given String is manipulated, a new
String
object is created with the manipulation.-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Puts to the given String at the start and end quotes.static String
Puts to the given String at the start and end quotes.static byte[]
convertToBytearray
(char[] source) Converts the given chararray to a bytearray.static char[]
convertToCharArray
(byte[] source) Convert the given byte array to char array.static Character
convertUnicodeStringToCharacter
(String theUnicodeString) Converts the given unicode String object to character object.filterStringsWithSeperator
(List<String> ignoreFieldNames, String separator) static String
firstCharacterToLowerCase
(String fieldName) Sets the first character from the given string to lower case and returns it.static String
firstCharacterToUpperCase
(String fieldName) Sets the first character from the given string to upper case and returns it.static String
getAttributName
(String name) Gets the Attribut-name without brackets.static String
Gets the first index from the brackets.static String
getItemNumber
(String name) Gets the ItemNumber from the String name.static String
getStringAfterUnderscore
(String element) The Method getStringAfterUnderscore(String) gets the substring after the first underscore.static String
getStringBeforeUnderscore
(String element) The Method getStringBeforeUnderscore(String) gets the substring before the first underscore.static String
Gets the value from the given map and if it does not exist or is empty the given default value will be returned.static boolean
isNullOrEmpty
(String str) The method isNullOrEmpty(String) checks if the given String is empty or null.static boolean
Checks if the given String is an Number.static String
lastCharacter
(String str) Gets the last character of the given String object.static String
lastCharacterToUpperCase
(String fieldName) Sets the last character from the given string to upper case and returns it.newCharacterList
(String input) Factory method for create newArrayList
of unique characters from the given textnewCharacterList
(String text, Comparator<Character> comparator) Factory method for create newArrayList
of unique characters from the given text sorted with the givenComparator
Note: This method can be used for a custom Comparator that have a defined order.static String
Reads the given String till it finds a carriage return and returns it.static String[]
removeEmptyString
(String[] words) Removes empty Strings from the given string array and gives it back.static String
Removes the first and the last character from the given Stringstatic String
removeNewlineCharacters
(String subject) Removes the newline characters from the given String.static String
Removes from the given String at the start and end quotes.static String
replaceAll
(String original, String[] findString, String replaceWith) Replaces all occurrences of a String within another String.static String
replaceAll
(String original, String findString, String replaceWith) Replaces all occurrences of a String within another String.static String
replaceAll
(String original, Map<String, String> replaceWith) Replaces all occurrences of a String within another String.static String
replaceEach
(String input, String searchRegexPattern, String replace) Replace each occurences from the search pattern(regex) with the given replace String of the given input String.static String
replaceLast
(String original, String findString, String replacement) Replaces the last occurrence of a String within another String.splitByFixedLength
(String input, int fixedLength) Split the givenString
in parts in the given fixed length.static int[]
toIntegerArray
(String integerArrayAsString, String delimiter) Transforms the givenString
that holds successive numbers delimited with a delimiter into an integer array.static String
Converts the given character list to a Stringstatic <T> String
toString
(T object) Prints theObject.toString()
and if the given object is null a corresponding information.static String
Converts all characters from the given String to unicodes characters encoded like \uxxxx.static String
toUnicodeChars
(String s, boolean toLowerCase) Converts all characters from the given String to unicodes characters encoded like \uxxxx.
-
Method Details
-
addDoubleQuotationMarks
Puts to the given String at the start and end quotes.- Parameters:
s
- The String to handle.- Returns:
- The String with quotes.
-
addSingleQuotationMarks
Puts to the given String at the start and end quotes.- Parameters:
s
- The String to handle.- Returns:
- The String with quotes.
-
convertToBytearray
public static byte[] convertToBytearray(char[] source) Converts the given chararray to a bytearray.- Parameters:
source
- the source.- Returns:
- the byte[]
-
convertToCharArray
public static char[] convertToCharArray(byte[] source) Convert the given byte array to char array.- Parameters:
source
- the source.- Returns:
- the char[]
-
convertUnicodeStringToCharacter
Converts the given unicode String object to character object.
Example: the given unicode String object is "\u03A9"
the return character object would be "Ω".- Parameters:
theUnicodeString
- the unicode string- Returns:
- the converted character object.
-
filterStringsWithSeperator
public static List<String> filterStringsWithSeperator(List<String> ignoreFieldNames, String separator) - Parameters:
ignoreFieldNames
- the ignore field namesseparator
- the separator- Returns:
- the filtered
List
-
firstCharacterToLowerCase
Sets the first character from the given string to lower case and returns it. Example:
Given fieldName: UserName
Result: userName- Parameters:
fieldName
- The String to modify.- Returns:
- The modified string.
-
firstCharacterToUpperCase
Sets the first character from the given string to upper case and returns it. Example:
Given fieldName: userName
Result: UserName- Parameters:
fieldName
- The String to modify.- Returns:
- The modified string.
-
getAttributName
Gets the Attribut-name without brackets. For example: If "indio[5][1]" is the given String than the name is "indio" and the first index is "5" and the second is "1". Than the Method returns "indio".- Parameters:
name
- The name with brackets.- Returns:
- The name without brackets.
-
getIndex
Gets the first index from the brackets. For example: If "indio[5][1]" is the given String than the name is "indio" and the first index is "5" and the second is "1". Than the Method returns "5".- Parameters:
name
- The name from the Attribute- Returns:
- the index from the Attribute
-
getItemNumber
Gets the ItemNumber from the String name. For example: If "indio[5][1]" is the given String than the name is "indio" and the first index is "5" and the second is "1". Than the Method returns "1".- Parameters:
name
- the name- Returns:
- the ItemNumber
-
getStringAfterUnderscore
The Method getStringAfterUnderscore(String) gets the substring after the first underscore. For example:Assume that element=foobar_value then the result string is "value".- Parameters:
element
- The element to get the substing.- Returns:
- The result String.
-
getStringBeforeUnderscore
The Method getStringBeforeUnderscore(String) gets the substring before the first underscore. For example:Assume that element=foobar_value then the result string is "foobar".- Parameters:
element
- The element to get the substing.- Returns:
- The result String.
-
getValue
Gets the value from the given map and if it does not exist or is empty the given default value will be returned.- Parameters:
data
- the datakey
- the keydefaultValue
- the default value- Returns:
- the value
-
isNullOrEmpty
The method isNullOrEmpty(String) checks if the given String is empty or null.- Parameters:
str
- The String to check.- Returns:
- true if the given String is empty or null otherwise false.
-
isNumber
Checks if the given String is an Number.- Parameters:
testString
- The String to check.- Returns:
- true if the given String is a number otherwise false.
-
lastCharacter
Gets the last character of the given String object.- Parameters:
str
- the str- Returns:
- the last character
-
lastCharacterToUpperCase
Sets the last character from the given string to upper case and returns it. Example:
Given fieldName: userName
Result: userNamE- Parameters:
fieldName
- The String to modify.- Returns:
- The modified string.
-
readLine
Reads the given String till it finds a carriage return and returns it.- Parameters:
stringWithCarriageReturns
- The String.- Returns:
- Return the String till the carriage return.
-
removeEmptyString
Removes empty Strings from the given string array and gives it back. For instance if you use the String.split("\\W") method you get all the words from the given String as an string array. But this array contains empty Strings too. This method removes this empty strings from that array.- Parameters:
words
- The string array to remove empty strings.- Returns:
- An string array without empty Strings.
-
removeFirstAndLastCharacter
Removes the first and the last character from the given String- Parameters:
s
- The String to handle- Returns:
- The resulted String
-
removeNewlineCharacters
Removes the newline characters from the given String.- Parameters:
subject
- the subject- Returns:
- the string
-
removeQuotationMarks
Removes from the given String at the start and end quotes.- Parameters:
s
- The String to handle.- Returns:
- The String without quotes.
-
replaceAll
Replaces all occurrences of a String within another String.- Parameters:
original
- The String to search.findString
- The search pattern.replaceWith
- Replace with that String.- Returns:
- Returns a new String with the replaced Strings.
-
replaceAll
Replaces all occurrences of a String within another String.- Parameters:
original
- The String to search.findString
- An array with search patterns.replaceWith
- Replace with that String.- Returns:
- Returns a new String with the replaced Strings.
-
replaceAll
Replaces all occurrences of a String within another String.- Parameters:
original
- The String to search.replaceWith
- Replace map with the specific replace values- Returns:
- Returns a new String with the replaced Strings.
-
replaceEach
Replace each occurences from the search pattern(regex) with the given replace String of the given input String.- Parameters:
input
- the input String that will be changed.searchRegexPattern
- the search regex patternreplace
- the String to replace with.- Returns:
- the resulted string
-
replaceLast
Replaces the last occurrence of a String within another String.- Parameters:
original
- The String to search.findString
- The search pattern.replacement
- Replace with that String.- Returns:
- Returns a new String with the replaced Strings.
-
splitByFixedLength
Split the givenString
in parts in the given fixed length.- Parameters:
input
- the inputfixedLength
- the fixed length- Returns:
- the list with the splitted
String
objects
-
toIntegerArray
public static int[] toIntegerArray(String integerArrayAsString, String delimiter) throws NumberFormatException Transforms the givenString
that holds successive numbers delimited with a delimiter into an integer array. Note: A validString
object holds only integer values delimited with the given delimiter. Example: A validString
object is "12, 13, 14, 16, 17, 18" that results in an integer array of [12, 13, 14, 16, 17, 18]- Parameters:
integerArrayAsString
- the integer array as stringdelimiter
- the delimiter- Returns:
- the created integer array from the given
String
- Throws:
NumberFormatException
- is thrown if the givenString
is not valid.
-
toString
Prints theObject.toString()
and if the given object is null a corresponding information.- Type Parameters:
T
- the generic type- Parameters:
object
- the object- Returns:
- the string
-
toUnicode
Converts all characters from the given String to unicodes characters encoded like \uxxxx.- Parameters:
toUnicode
- The String to convert.toLowerCase
- If true the letters from the unicode characters are lower case.- Returns:
- The converted String.
-
toUnicodeChars
Converts all characters from the given String to unicodes characters encoded like \uxxxx.- Parameters:
s
- The String to convert.toLowerCase
- If true the letters from the unicode characters are lower case.- Returns:
- The converted String.
-
newCharacterList
Factory method for create newArrayList
of unique characters from the given text- Parameters:
input
- the input text- Returns:
- the new
List
with the unique characters
-
newCharacterList
Factory method for create newArrayList
of unique characters from the given text sorted with the givenComparator
Note: This method can be used for a custom Comparator that have a defined order. For example:// defined custom order List<Character> definedOrder = Arrays.asList('c', 'b', 'a', 'd', '.', ...); Comparator<Character> customComparator = Comparator.comparing(character -> definedOrder.indexOf(character));
- Parameters:
text
- the textcomparator
- the comparator- Returns:
- the new
List
with the unique characters
-
toString
Converts the given character list to a String- Parameters:
characterList
- the list of characters to convert- Returns:
- the new
String
from the given character list
-