Simple example of determining the length of Chinese string in java
Without further ado, here’s the code:
/** * 获取字符串的长度,如果有中文,则每个中文字符计为2位 * @param value 指定的字符串 * @return 字符串的长度 */ public static int length(String value) { int valueLength = 0; String chinese = "[\u0391-\uFFE5]"; /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */ for (int i = 0; i < value.length(); i++) { /* 获取一个字符 */ String temp = value.substring(i, i + 1); /* 判断是否为中文字符 */ if (temp.matches(chinese)) { /* 中文字符长度为2 */ valueLength += 2; } else { /* 其他字符长度为1 */ valueLength += 1; } } return valueLength; } /** * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1 * @param String s 需要得到长度的字符串 * @return int 得到的字符串长度 */ public static int length(String s) { if (s == null) return 0; char[] c = s.toCharArray(); int len = 0; for (int i = 0; i < c.length; i++) { len++; if (!isLetter(c[i])) { len++; } } return len; } /** * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为1,英文字符长度为0.5 * @param String s 需要得到长度的字符串 * @return int 得到的字符串长度 */ public static double getLength(String s) { double valueLength = 0; String chinese = "[\u4e00-\u9fa5]"; // 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 for (int i = 0; i < s.length(); i++) { // 获取一个字符 String temp = s.substring(i, i + 1); // 判断是否为中文字符 if (temp.matches(chinese)) { // 中文字符长度为1 valueLength += 1; } else { // 其他字符长度为0.5 valueLength += 0.5; } } //进位取整 return Math.ceil(valueLength); } 根据长度截取内容,区分中英文: /** * 截取字符长度,区分中英文 * * @param abc 字符串内容 * @param len 截取长度 * @return */ public static String subStr(String abc, int len) { if (TextUtils.isEmpty(abc) || len <= 0) return ""; StringBuffer stringBuffer = new StringBuffer(); int sum = 0; char[] chars = abc.toCharArray(); for (int i = 0; i < chars.length; i++) { if (sum >= (len * 3)) { break; } char bt = chars[i]; if (bt > 64 && bt < 123) { stringBuffer.append(String.valueOf(bt)); sum += 2; } else { stringBuffer.append(String.valueOf(bt)); sum += 3; } } return stringBuffer.toString(); }
The above simple example of using Java to determine the length of a Chinese string is all the content shared by the editor. I hope it can give you a reference. I also hope that Please support PHP Chinese website.
For more articles related to simple examples of using Java to determine the length of Chinese strings, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
