StringUtil
class provides a collection of utility methods for
performing common operations on strings. This class includes methods for
counting characters in a list of words, converting a string to an array of
characters, and calculating the similarity between two strings.
These utility methods are designed to simplify string manipulation tasks that
are frequently needed in various parts of an application. The methods are
static, allowing them to be called without creating an instance of the
StringUtil
class.
The StringUtil
class is intended to be used wherever string
operations are required, providing a centralised set of tools for common
string-related tasks.
This class is part of the com.slinky.hackmaster.util
package.
- Author:
- Kheagen Haskins
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
calculateSimilarity
(String str1, String str2) Calculates the similarity between two strings by counting the number of matching characters at the same positions in both strings.static String
colorToRgbString
(javafx.scene.paint.Color color) Converts aColor
object into its RGB string representation.static int
countCharacters
(String[] wordList) Calculates and returns the total number of characters across all words in the word list.static Character[]
toCharacterArray
(String str) Converts the specifiedString
into an array ofCharacter
objects.
-
Constructor Details
-
StringUtil
public StringUtil()
-
-
Method Details
-
countCharacters
Calculates and returns the total number of characters across all words in the word list.This method iterates through each word in the
wordList
and sums up their lengths to determine the total number of characters. This can be useful for operations where the cumulative length of the words is required, such as when determining the size of a jumbled string.- Parameters:
wordList
- The list of words in which to count all characters- Returns:
- the total number of characters in all words within this
StaticWordSet
.
-
toCharacterArray
Converts the specifiedString
into an array ofCharacter
objects. Each character in the provided string is placed into the array at the corresponding index.- Parameters:
str
- the string to convert into an array ofCharacter
; should not be null as it would cause aNullPointerException
- Returns:
- an array of
Character
objects representing the characters of the input string - Throws:
NullPointerException
- if the input string isnull
-
calculateSimilarity
Calculates the similarity between two strings by counting the number of matching characters at the same positions in both strings.The method compares the characters of the two strings up to the length of the shorter string. For each character that matches at the same position in both strings, the similarity score is incremented by one.
- Parameters:
str1
- the first string to comparestr2
- the second string to compare- Returns:
- the number of matching characters at the same positions in both strings
-
colorToRgbString
Converts aColor
object into its RGB string representation. This method is used to create color strings in the "rgb(r, g, b)" format.- Parameters:
color
- theColor
object to be converted.- Returns:
- a
String
representing the color in RGB format.
-