StringUtil.java

net.minecraft.util.StringUtil

信息

  • 全限定名:net.minecraft.util.StringUtil
  • 类型:public class
  • 包:net.minecraft.util
  • 源码路径:src/main/java/net/minecraft/util/StringUtil.java
  • 起始行号:L9
  • 职责:

    TODO

字段/常量

  • STRIP_COLOR_PATTERN

    • 类型: Pattern
    • 修饰符: private static final
    • 源码定位: L10
    • 说明:

      TODO

  • LINE_PATTERN

    • 类型: Pattern
    • 修饰符: private static final
    • 源码定位: L11
    • 说明:

      TODO

  • LINE_END_PATTERN

    • 类型: Pattern
    • 修饰符: private static final
    • 源码定位: L12
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static String formatTickDuration(int ticks, float tickrate) @ L14

  • 方法名:formatTickDuration
  • 源码定位:L14
  • 返回类型:String
  • 修饰符:public static

参数:

  • ticks: int
  • tickrate: float

说明:

TODO

public static String stripColor(String input) @ L23

  • 方法名:stripColor
  • 源码定位:L23
  • 返回类型:String
  • 修饰符:public static

参数:

  • input: String

说明:

TODO

public static boolean isNullOrEmpty(String s) @ L27

  • 方法名:isNullOrEmpty
  • 源码定位:L27
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • s: String

说明:

TODO

public static String truncateStringIfNecessary(String s, int maxLength, boolean addDotDotDotIfTruncated) @ L31

  • 方法名:truncateStringIfNecessary
  • 源码定位:L31
  • 返回类型:String
  • 修饰符:public static

参数:

  • s: String
  • maxLength: int
  • addDotDotDotIfTruncated: boolean

说明:

TODO

public static int lineCount(String s) @ L39

  • 方法名:lineCount
  • 源码定位:L39
  • 返回类型:int
  • 修饰符:public static

参数:

  • s: String

说明:

TODO

public static boolean endsWithNewLine(String s) @ L54

  • 方法名:endsWithNewLine
  • 源码定位:L54
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • s: String

说明:

TODO

public static String trimChatMessage(String message) @ L58

  • 方法名:trimChatMessage
  • 源码定位:L58
  • 返回类型:String
  • 修饰符:public static

参数:

  • message: String

说明:

TODO

public static boolean isAllowedChatCharacter(int ch) @ L62

  • 方法名:isAllowedChatCharacter
  • 源码定位:L62
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • ch: int

说明:

TODO

public static boolean isValidPlayerName(String name) @ L66

  • 方法名:isValidPlayerName
  • 源码定位:L66
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • name: String

说明:

TODO

public static String filterText(String input) @ L70

  • 方法名:filterText
  • 源码定位:L70
  • 返回类型:String
  • 修饰符:public static

参数:

  • input: String

说明:

TODO

public static String filterText(String input, boolean multiline) @ L74

  • 方法名:filterText
  • 源码定位:L74
  • 返回类型:String
  • 修饰符:public static

参数:

  • input: String
  • multiline: boolean

说明:

TODO

public static boolean isWhitespace(int codepoint) @ L88

  • 方法名:isWhitespace
  • 源码定位:L88
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • codepoint: int

说明:

TODO

public static boolean isBlank(String string) @ L92

  • 方法名:isBlank
  • 源码定位:L92
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • string: String

说明:

TODO

代码

public class StringUtil {
    private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)\\u00A7[0-9A-FK-OR]");
    private static final Pattern LINE_PATTERN = Pattern.compile("\\r\\n|\\v");
    private static final Pattern LINE_END_PATTERN = Pattern.compile("(?:\\r\\n|\\v)$");
 
    public static String formatTickDuration(int ticks, float tickrate) {
        int seconds = Mth.floor(ticks / tickrate);
        int minutes = seconds / 60;
        seconds %= 60;
        int hours = minutes / 60;
        minutes %= 60;
        return hours > 0 ? String.format(Locale.ROOT, "%02d:%02d:%02d", hours, minutes, seconds) : String.format(Locale.ROOT, "%02d:%02d", minutes, seconds);
    }
 
    public static String stripColor(String input) {
        return STRIP_COLOR_PATTERN.matcher(input).replaceAll("");
    }
 
    public static boolean isNullOrEmpty(@Nullable String s) {
        return StringUtils.isEmpty(s);
    }
 
    public static String truncateStringIfNecessary(String s, int maxLength, boolean addDotDotDotIfTruncated) {
        if (s.length() <= maxLength) {
            return s;
        } else {
            return addDotDotDotIfTruncated && maxLength > 3 ? s.substring(0, maxLength - 3) + "..." : s.substring(0, maxLength);
        }
    }
 
    public static int lineCount(String s) {
        if (s.isEmpty()) {
            return 0;
        } else {
            Matcher matcher = LINE_PATTERN.matcher(s);
            int count = 1;
 
            while (matcher.find()) {
                count++;
            }
 
            return count;
        }
    }
 
    public static boolean endsWithNewLine(String s) {
        return LINE_END_PATTERN.matcher(s).find();
    }
 
    public static String trimChatMessage(String message) {
        return truncateStringIfNecessary(message, 256, false);
    }
 
    public static boolean isAllowedChatCharacter(int ch) {
        return ch != 167 && ch >= 32 && ch != 127;
    }
 
    public static boolean isValidPlayerName(String name) {
        return name.length() > 16 ? false : name.chars().filter(c -> c <= 32 || c >= 127).findAny().isEmpty();
    }
 
    public static String filterText(String input) {
        return filterText(input, false);
    }
 
    public static String filterText(String input, boolean multiline) {
        StringBuilder builder = new StringBuilder();
 
        for (char c : input.toCharArray()) {
            if (isAllowedChatCharacter(c)) {
                builder.append(c);
            } else if (multiline && c == '\n') {
                builder.append(c);
            }
        }
 
        return builder.toString();
    }
 
    public static boolean isWhitespace(int codepoint) {
        return Character.isWhitespace(codepoint) || Character.isSpaceChar(codepoint);
    }
 
    public static boolean isBlank(@Nullable String string) {
        return string != null && !string.isEmpty() ? string.chars().allMatch(StringUtil::isWhitespace) : true;
    }
}

引用的其他类

  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.floor()
  • TropicalFish

    • 引用位置: 字段/方法调用
    • 关联成员: Pattern.compile()