Java string intercept class distinguishes Chinese, English, numerals, punctuation

Source: Internet
Author: User

Package com.founder.fix.ims;

/**
* @author Wangyutao
* Manipulating strings
*/

public class SubString {

public static void Main (string[] args) {
String str = "I love Aston when the Grand Master Dafa God swore that the local times took place in the Grand Master Dafa Gold";
String str1 = "SDFDSSFSFDSF Dsdafdafafdsfadas";
SYSTEM.OUT.PRINTLN (mysubstring (str, 14));
System.out.println (Mysubstring (STR1, 14));
// }

/**
* Determine whether a character is a ascill character or another character (such as Han, Japanese, Korean characters)
*
* @param c
* @return
*/
public static Boolean Isletter (char c) {
int k = 0x80;
return (c/k) = = 0? True:false;
}

/**
* Gets the length of a string, the length of the display, a Chinese character or a Japanese-Korean length of 2, an English character length of 1
*
* @param String
* s, need to get the length of the string
* @return int, the resulting string length
*/
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;
}

/**
* Gets the length of a string, the length of the display, a Chinese character or a Japanese-Korean length of 1, an English character length of 0.5
*
* @param string s need to get length strings
* The string length obtained @return int
*/
public static double GetLength (String s) {
Double valuelength = 0;
String Chinese = "[\u4e00-\u9fa5]";
Gets the length of the field value, if it contains Chinese characters, the length of each Chinese character is 2, otherwise 1
for (int i = 0; i < s.length (); i++) {
Get a character
String temp = s.substring (i, i + 1);
Determines whether to be Chinese characters
if (Temp.matches (Chinese)) {
Chinese character length is 1
Valuelength + = 1;
} else {
Other character lengths are 0.5
Valuelength + = 0.5;
}
}
Carry rounding
Return Math.ceil (valuelength);
}

/**
* Intercept the length of a character, do not distinguish between Chinese and English, if the number is not exactly, then take one less character bit
*
* @author Patriotlml
* @param String
* Origin, original string
* @param int
* len, intercept length (a Chinese character length is 2)
* @return string, returned by strings
*/
public static string getsubstring (string origin, int len) {
if (origin = = null) | | origin.equals ("") | | (Len < 1)) {
Return "";
}
byte[] Strbyte = new Byte[len];
int relen = 0;
if (Len > Length (Origin)) {
return origin;
}
System.arraycopy (Origin.getbytes (), 0, strbyte, 0, Len);
int count = 0;
for (int i = 0; i < len; i++) {
int value = Strbyte[i];
if (value < 0) {
count++;
}
}
if ((count% 2)! = 0) {
Relen = (len = = 1)? ++len:--len;
}
return new String (strbyte, 0, Relen);
}
}

Java string intercept class distinguishes Chinese, English, numerals, punctuation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.