Class FormatUtils

  • All Implemented Interfaces:
    Utils

    public abstract class FormatUtils
    extends java.lang.Object
    implements Utils
    The utility class of text format
    Since:
    1.0.0
    Author:
    Mercy
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_PLACEHOLDER  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String format​(java.lang.String pattern, java.lang.Object... args)
      Formats the given pattern by replacing all occurrences of the default placeholder (DEFAULT_PLACEHOLDER) with corresponding values from the provided arguments.
      static java.lang.String formatWithPlaceholder​(java.lang.String pattern, java.lang.String placeholder, java.lang.Object... args)
      Formats the given pattern by replacing all occurrences of the specified placeholder with corresponding values from the provided arguments.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_PLACEHOLDER

        public static final java.lang.String DEFAULT_PLACEHOLDER
        See Also:
        Constant Field Values
    • Method Detail

      • format

        public static java.lang.String format​(java.lang.String pattern,
                                              java.lang.Object... args)
        Formats the given pattern by replacing all occurrences of the default placeholder (DEFAULT_PLACEHOLDER) with corresponding values from the provided arguments.

        If there are more placeholders in the pattern than arguments provided, extra placeholders will remain unchanged.

        Example Usage

        
         format("Hello, {}", "World") => "Hello, World"
         format("{} + {} = 4", 2, 2) => "2 + 2 = 4"
         format("No replacement here", "extra") => "No replacement here"
         
        Parameters:
        pattern - the string pattern to be formatted
        args - the arguments to replace the placeholders in the pattern
        Returns:
        the formatted string, with placeholders replaced by corresponding argument values
      • formatWithPlaceholder

        public static java.lang.String formatWithPlaceholder​(java.lang.String pattern,
                                                             java.lang.String placeholder,
                                                             java.lang.Object... args)
        Formats the given pattern by replacing all occurrences of the specified placeholder with corresponding values from the provided arguments.

        If there are more placeholders in the pattern than arguments provided, extra placeholders will remain unchanged.

        Example Usage

        
         formatWithPlaceholder("Hello, [placeholder]", "[placeholder]", "World") => "Hello, World"
         formatWithPlaceholder("[placeholder] + [placeholder] = 4", "[placeholder]", 2, 2) => "2 + 2 = 4"
         formatWithPlaceholder("No replacement here", "[placeholder]", "extra") => "No replacement here"
         
        Parameters:
        pattern - the string pattern to be formatted
        placeholder - the placeholder string to be replaced
        args - the arguments to replace the placeholders in the pattern
        Returns:
        the formatted string, with placeholders replaced by corresponding argument values