有没有类似 isBlank() 的工具可以用于存储密码的字符数组?

huangapple go评论59阅读模式
英文:

Is there an isBlank() like utility for char array to be used for storing a password

问题

StringUtilsisBlank()方法。char[]有类似的东西吗,可以合并检查空白、null和空格吗?

英文:

StringUtils has the isBlank() method. Does char[] have something similar that consolidates checking empty, null and whitespace(s)?

答案1

得分: 4

这里没有内置功能来进行此特定检查,但你可以自己编写一个。StringUtils.isBlank() 会修剪 String,这可能不是你想要的,你可以直接检查数组:

public static boolean isBlank(char[] arr) {
  if (arr == null) {
    return true; // StringUtils.isBlank() 对于空字符串返回true
  }
  for (char c : arr) {
    if (!Character.isWhitespace(c)) {
      return false;
    }
  }
  return true;
}
英文:

There is nothing build in for this specific check but you can write one yourself. The StringUtils.isBlank() trims the String which you probably want to avoid and instead check the array directly:

public static boolean isBlank(char[] arr) {
  if (arr == null) {
    return true; // StringUtils.isBlank() returs true for null String
  }
  for (char c : arr) {
    if (!Character.isWhitespace(c)) {
      return false;
    }
  }
  return true;
}

huangapple
  • 本文由 发表于 2020年9月8日 19:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63793128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定