Package com.cedarsoftware.util
Class StringUtilities
- java.lang.Object
-
- com.cedarsoftware.util.StringUtilities
-
public final class StringUtilities extends Object
Useful String utilities for common tasks- Author:
- Ken Partlow, John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
-
Field Summary
Fields Modifier and Type Field Description static String
FOLDER_SEPARATOR
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
count(String s, char c)
static String
createString(byte[] bytes, String encoding)
Convert a byte[] into a String with a particular encoding.static String
createUtf8String(byte[] bytes)
Convert a byte[] into a UTF-8 String.static String
createUTF8String(byte[] bytes)
Convert a byte[] into a UTF-8 encoded String.static int
damerauLevenshteinDistance(CharSequence source, CharSequence target)
Calculate the Damerau-Levenshtein Distance between two strings.static byte[]
decode(String s)
static String
encode(byte[] bytes)
Convert a byte array into a printable format containing a String of hex digit characters (two per byte).static boolean
equals(String str1, String str2)
static boolean
equalsIgnoreCase(String s1, String s2)
static boolean
equalsIgnoreCaseWithTrim(String s1, String s2)
static boolean
equalsWithTrim(String s1, String s2)
static byte[]
getBytes(String s, String encoding)
Convert a String into a byte[] with a particular encoding.static String
getRandomChar(Random random, boolean upper)
static String
getRandomString(Random random, int minLen, int maxLen)
static byte[]
getUTF8Bytes(String s)
Convert a String into a byte[] encoded by UTF-8.static boolean
hasContent(String s)
static int
hashCodeIgnoreCase(String s)
Get the hashCode of a String, insensitive to case, without any new Strings being created on the heap.static boolean
isEmpty(String s)
static int
lastIndexOf(String path, char ch)
static int
length(String s)
Use this method when you don't want a length check to throw a NullPointerException whenstatic int
levenshteinDistance(CharSequence s, CharSequence t)
The Levenshtein distance is a string metric for measuring the difference between two sequences.static int
trimLength(String s)
Returns the length of the trimmed string.static String
wildcardToRegexString(String wildcard)
Convert strings containing DOS-style '*' or '?' to a regex String.
-
-
-
Field Detail
-
FOLDER_SEPARATOR
public static final String FOLDER_SEPARATOR
- See Also:
- Constant Field Values
-
-
Method Detail
-
isEmpty
public static boolean isEmpty(String s)
-
hasContent
public static boolean hasContent(String s)
-
length
public static int length(String s)
Use this method when you don't want a length check to throw a NullPointerException when- Parameters:
s
- string to return length of- Returns:
- 0 if string is null, otherwise the length of string.
-
trimLength
public static int trimLength(String s)
Returns the length of the trimmed string. If the length is null then it returns 0.
-
lastIndexOf
public static int lastIndexOf(String path, char ch)
-
decode
public static byte[] decode(String s)
-
encode
public static String encode(byte[] bytes)
Convert a byte array into a printable format containing a String of hex digit characters (two per byte).- Parameters:
bytes
- array representation
-
count
public static int count(String s, char c)
-
wildcardToRegexString
public static String wildcardToRegexString(String wildcard)
Convert strings containing DOS-style '*' or '?' to a regex String.
-
levenshteinDistance
public static int levenshteinDistance(CharSequence s, CharSequence t)
The Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one word into the other. The phrase 'edit distance' is often used to refer specifically to Levenshtein distance.- Parameters:
s
- String onet
- String two- Returns:
- the 'edit distance' (Levenshtein distance) between the two strings.
-
damerauLevenshteinDistance
public static int damerauLevenshteinDistance(CharSequence source, CharSequence target)
Calculate the Damerau-Levenshtein Distance between two strings. The basic difference between this algorithm and the general Levenshtein algorithm is that damerau-Levenshtein counts a swap of two characters next to each other as 1 instead of 2. This breaks the 'triangular equality', which makes it unusable for Metric trees. See Wikipedia pages on both Levenshtein and Damerau-Levenshtein and then make your decision as to which algorithm is appropriate for your situation.- Parameters:
source
- Source input stringtarget
- Target input string- Returns:
- The number of substitutions it would take to make the source string identical to the target string
-
getRandomString
public static String getRandomString(Random random, int minLen, int maxLen)
- Parameters:
random
- Random instanceminLen
- minimum number of charactersmaxLen
- maximum number of characters- Returns:
- String of alphabetical characters, with the first character uppercase (Proper case strings).
-
getBytes
public static byte[] getBytes(String s, String encoding)
Convert a String into a byte[] with a particular encoding. Preferable used when the encoding is one of the guaranteed Java types and you don't want to have to catch the UnsupportedEncodingException required by Java- Parameters:
s
- string to encode into bytesencoding
- encoding to use
-
createUtf8String
public static String createUtf8String(byte[] bytes)
Convert a byte[] into a UTF-8 String. Preferable used when the encoding is one of the guaranteed Java types and you don't want to have to catch the UnsupportedEncodingException required by Java- Parameters:
bytes
- bytes to encode into a string
-
getUTF8Bytes
public static byte[] getUTF8Bytes(String s)
Convert a String into a byte[] encoded by UTF-8.- Parameters:
s
- string to encode into bytes
-
createString
public static String createString(byte[] bytes, String encoding)
Convert a byte[] into a String with a particular encoding. Preferable used when the encoding is one of the guaranteed Java types and you don't want to have to catch the UnsupportedEncodingException required by Java- Parameters:
bytes
- bytes to encode into a stringencoding
- encoding to use
-
createUTF8String
public static String createUTF8String(byte[] bytes)
Convert a byte[] into a UTF-8 encoded String.- Parameters:
bytes
- bytes to encode into a string
-
hashCodeIgnoreCase
public static int hashCodeIgnoreCase(String s)
Get the hashCode of a String, insensitive to case, without any new Strings being created on the heap.- Parameters:
s
- String input- Returns:
- int hashCode of input String insensitive to case
-
-