Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.4 kB
2
Indexable
Never
public class StringManipulation {

    public String formatString(String format, Object... args) {
        StringBuilder result = new StringBuilder();
        int argIndex = 0;
        
        for (int i = 0; i < format.length(); i++) {
            if (format.charAt(i) == '%' && i + 2 < format.length()) {
                char nextChar = format.charAt(i + 1);
                char specifier = format.charAt(i + 2);
                
                if ((nextChar == 's' || nextChar == 'd' || nextChar == 'f') && (specifier == '.')) {
                    String argString = args[argIndex++].toString();
                    int precision = Character.getNumericValue(format.charAt(i + 3));
                    
                    if (argString.contains(".")) {
                        int dotIndex = argString.indexOf(".");
                        argString = argString.substring(0, dotIndex + precision + 1);
                    }
                    
                    int width = Character.getNumericValue(format.charAt(i - 1));
                    while (argString.length() < width) {
                        argString = " " + argString;
                    }
                    
                    result.append(argString);
                    i += 4; // Пропустить следующие символы
                } else {
                    result.append(format.charAt(i));
                }
            } else {
                result.append(format.charAt(i));
            }
        }
        
        return result.toString();
    }

    public boolean startsWith(String str, String prefix) {
      for (int i = 0; i < prefix.length(); i++) {
          if (str.charAt(i) != prefix.charAt(i)) {
              return false;
          }
      }
      return true;
    }

    public boolean endsWith(String str, String suffix) {
      for (int i = 0; i < suffix.length(); i++) {
          if (str.charAt(str.length() - 1 - i) != suffix.charAt(suffix.length() - 1 - i)) {
              return false;
          }
      }
      return true;
    }

    public String toLowerCase(String str) {
      StringBuilder result = new StringBuilder();
      for (char c : str.toCharArray()) {
          if ('A' <= c && c <= 'Z') {
              result.append((char)(c + ('a' - 'A')));
          } else {
              result.append(c);
          }
      }
      return result.toString();
    }

    public String toUpperCase(String str) {
      StringBuilder result = new StringBuilder();
      for (char c : str.toCharArray()) {
          if ('a' <= c && c <= 'z') {
              result.append((char)(c - ('a' - 'A')));
          } else {
              result.append(c);
          }
      }
      return result.toString();
    }

    public String trim(String str) {
      int start = 0, end = str.length() - 1;
      while (start < end && str.charAt(start) == ' ') start++;
      while (start < end && str.charAt(end) == ' ') end--;
      return str.substring(start, end + 1);
    }

    public int compareTo(String str1, String str2) {
      int len1 = str1.length(), len2 = str2.length();
      for (int i = 0; i < Math.min(len1, len2); i++) {
          if (str1.charAt(i) != str2.charAt(i)) {
              return str1.charAt(i) - str2.charAt(i);
          }
      }
      return len1 - len2;
    }
}
Leave a Comment