Class StringUtil

java.lang.Object
com.slinky.hackmaster.util.StringUtil

public class StringUtil extends Object
The 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 Details

    • StringUtil

      public StringUtil()
  • Method Details

    • countCharacters

      public static int countCharacters(String[] wordList)
      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

      public static Character[] toCharacterArray(String str)
      Converts the specified String into an array of Character 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 of Character; should not be null as it would cause a NullPointerException
      Returns:
      an array of Character objects representing the characters of the input string
      Throws:
      NullPointerException - if the input string is null
    • calculateSimilarity

      public static 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.

      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 compare
      str2 - the second string to compare
      Returns:
      the number of matching characters at the same positions in both strings
    • colorToRgbString

      public static String colorToRgbString(javafx.scene.paint.Color color)
      Converts a Color object into its RGB string representation. This method is used to create color strings in the "rgb(r, g, b)" format.
      Parameters:
      color - the Color object to be converted.
      Returns:
      a String representing the color in RGB format.