首页 > 编程知识 正文

字符串长度不够时 根据字符长度补空格,java空格算字符长度吗

时间:2023-05-04 23:25:57 阅读:227203 作者:2952

字符串长度不够时  根据字符长度补空格  public static String String_length(String value,int totalLen) { int valueLength = 0; String chinese = "[u4e00-u9fa5]"; for (int i = 0; i < value.length(); i++) { String temp = value.substring(i, i + 1); if (temp.matches(chinese)) { valueLength += 2; } else { valueLength += 1; } } if (valueLength < totalLen) { int temp = totalLen - valueLength; for (int i = 0; i < temp; i++) { value+= " "; } } return value;} 用法 :效果 字符串长度不够时  根据字符串长度补空格 String.format("%-12s","aaa")

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。