Package org.eclipse.lemminx.utils
Class StringUtils
- java.lang.Object
-
- org.eclipse.lemminx.utils.StringUtils
-
public class StringUtils extends Object
String utilities.
-
-
Field Summary
Fields Modifier and Type Field Description static String[]EMPTY_STRINGstatic StringFALSEstatic StringTRUEstatic Collection<String>TRUE_FALSE_ARRAY
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringcleanPathForWindows(String pathString)static StringconvertToQuotelessValue(String value)Returnsvaluewithout surrounding quotes.static StringescapeBackticks(String text)static intfindEndWord(String text, int offset, int max, Predicate<Character> isValidChar)Returns the end word offset from the right of the givenoffsetto the givenmaxand -1 if no word.static intfindEndWord(String text, int offset, Predicate<Character> isValidChar)Returns the end word offset from the right of the givenoffsetand -1 if no word.static intfindStartWord(String text, int offset, int min, Predicate<Character> isValidChar)Returns the start word offset from the left of the givenoffsetto the givenminand -1 if no word.static intfindStartWord(String text, int offset, Predicate<Character> isValidChar)Returns the start word offset from the left of the givenoffsetand -1 if no word.static StringgetDefaultString(String text)Given a string will give back a non null string that is either the given string, or an empty string.static intgetFrontWhitespaceLength(String text)Returns the number of consecutive whitespace characters in front of textstatic intgetNumberOfNewLines(String text, boolean isWhitespace, String delimiter, int newLineLimit)Given a string that is only whitespace, this will return the amount of newline characters.static intgetOffsetAfterWhitespace(String text, int endOffset)Traverses backwards from the endOffset until it finds a whitespace character.static StringgetStartWhitespaces(String lineText)Returns the start whitespaces of the given line text.static StringgetString(Object obj)static intgetTrailingWhitespaceLength(String text)Returns the number of consecutive whitespace characters from the end of textstatic StringgetWhitespaces(String text, int start, int end)Returns the whitespaces from the given range start/end of the given text.static booleanisBlank(String value)Checks if a string is null or consists of only whitespace characters.static booleanisEmpty(String value)static booleanisQuote(char c)static booleanisQuoted(String value)Returns true ifvaluehas matching surrounding quotes and false otherwise.static booleanisSimilar(String reference, String current)Uses Levenshtein distance to determine similarity between stringsstatic booleanisTagOutsideOfBackticks(String text)static booleanisWhitespace(String value)static booleanisWhitespace(String value, int index)static booleanisWhitespace(String value, int index, int end)static StringlTrim(String value)static StringnormalizeSpace(String str)Returns the result of normalize space of the given string.static voidnormalizeSpace(String str, StringBuilder b)Normalizes the whitespace characters of a given string and applies it to the given string builder.static StringtrimNewLines(String value)static voidtrimNewLines(String value, StringBuilder s)
-
-
-
Field Detail
-
EMPTY_STRING
public static final String[] EMPTY_STRING
-
TRUE
public static final String TRUE
- See Also:
- Constant Field Values
-
FALSE
public static final String FALSE
- See Also:
- Constant Field Values
-
TRUE_FALSE_ARRAY
public static final Collection<String> TRUE_FALSE_ARRAY
-
-
Method Detail
-
isEmpty
public static boolean isEmpty(String value)
-
isQuote
public static boolean isQuote(char c)
-
isWhitespace
public static boolean isWhitespace(String value, int index)
-
isWhitespace
public static boolean isWhitespace(String value, int index, int end)
-
isWhitespace
public static boolean isWhitespace(String value)
-
isBlank
public static boolean isBlank(String value)
Checks if a string is null or consists of only whitespace characters.- Parameters:
value- The string to check- Returns:
trueif any of the below hold, and false otherwise:- The String is
null - The String is of length 0
- The String contains only whitespace characters
- The String is
-
normalizeSpace
public static void normalizeSpace(String str, StringBuilder b)
Normalizes the whitespace characters of a given string and applies it to the given string builder.- Parameters:
str-
-
normalizeSpace
public static String normalizeSpace(String str)
Returns the result of normalize space of the given string.- Parameters:
str-- Returns:
- the result of normalize space of the given string.
-
getStartWhitespaces
public static String getStartWhitespaces(String lineText)
Returns the start whitespaces of the given line text.- Parameters:
lineText-- Returns:
- the start whitespaces of the given line text.
-
getWhitespaces
public static String getWhitespaces(String text, int start, int end)
Returns the whitespaces from the given range start/end of the given text.- Parameters:
start- the range startend- the range endtext- the text- Returns:
- the whitespaces from the given range start/end of the given text.
-
trimNewLines
public static void trimNewLines(String value, StringBuilder s)
-
getNumberOfNewLines
public static int getNumberOfNewLines(String text, boolean isWhitespace, String delimiter, int newLineLimit)
Given a string that is only whitespace, this will return the amount of newline characters. If the newLineCounter becomes > newLineLimit, then the value of newLineLimit is always returned.- Parameters:
text-isWhitespace-delimiter-- Returns:
-
getDefaultString
public static String getDefaultString(String text)
Given a string will give back a non null string that is either the given string, or an empty string.- Parameters:
text-- Returns:
-
getOffsetAfterWhitespace
public static int getOffsetAfterWhitespace(String text, int endOffset)
Traverses backwards from the endOffset until it finds a whitespace character. The offset of the character after the whitespace is returned. (text = "abcd efg|h", endOffset = 8) -> 5- Parameters:
text-endOffset- non-inclusive- Returns:
- Start offset directly after the first whitespace.
-
getFrontWhitespaceLength
public static int getFrontWhitespaceLength(String text)
Returns the number of consecutive whitespace characters in front of text- Parameters:
text- String of interest- Returns:
- the number of consecutive whitespace characters in front of text
-
getTrailingWhitespaceLength
public static int getTrailingWhitespaceLength(String text)
Returns the number of consecutive whitespace characters from the end of text- Parameters:
text- String of interest- Returns:
- the number of consecutive whitespace characters from the end of text
-
isTagOutsideOfBackticks
public static boolean isTagOutsideOfBackticks(String text)
-
findStartWord
public static int findStartWord(String text, int offset, Predicate<Character> isValidChar)
Returns the start word offset from the left of the givenoffsetand -1 if no word.- Parameters:
text- the textoffset- the offsetisValidChar- predicate to check if current character belong to the word.- Returns:
- the start word offset from the left of the given
offsetand -1 if no word.
-
findStartWord
public static int findStartWord(String text, int offset, int min, Predicate<Character> isValidChar)
Returns the start word offset from the left of the givenoffsetto the givenminand -1 if no word.- Parameters:
text- the textoffset- the offsetmin- the minimum left offset.isValidChar- predicate to check if current character belong to the word.- Returns:
- the start word offset from the left of the given
offsetto the givenminand -1 if no word.
-
findEndWord
public static int findEndWord(String text, int offset, Predicate<Character> isValidChar)
Returns the end word offset from the right of the givenoffsetand -1 if no word.- Parameters:
text- the textoffset- the offsetisValidChar- predicate to check if current character belong to the word.- Returns:
- the start word offset from the right of the given
offsetand -1 if no word.
-
findEndWord
public static int findEndWord(String text, int offset, int max, Predicate<Character> isValidChar)
Returns the end word offset from the right of the givenoffsetto the givenmaxand -1 if no word.- Parameters:
text- the textoffset- the offsetisValidChar- predicate to check if current character belong to the word.- Returns:
- the start word offset from the right of the given
offsetand -1 if no word.
-
convertToQuotelessValue
public static String convertToQuotelessValue(String value)
Returnsvaluewithout surrounding quotes. Ifvaluedoes not have matching surrounding quotes, returnsvalue.- Parameters:
value-- Returns:
valuewithout surrounding quotes.
-
isQuoted
public static boolean isQuoted(String value)
Returns true ifvaluehas matching surrounding quotes and false otherwise.- Parameters:
value-- Returns:
- true if
valuehas matching surrounding quotes.
-
-