Java String Length Truncation

Source: Internet
Author: User

Java String Length Truncation

The Code is as follows:

/*** String Length truncation * @ author Administrator **/public class Main {public static void main (String [] args) {String str = "java tutorial network reminds you, the full-screen viewing is better and more HD. This video is intended for learning and does not include any business models and does not provide any technical services. If you have any other questions, contact the website administrator, more java source code is available in source code heaven. "; Int len = str. length (), maxLen = 62, temp = 0, I = 0; for (; I <len & temp <= maxLen; I ++) {char c = str. charAt (I); int charLen = String. valueOf (c ). getBytes (). length; temp + = charLen; System. out. println (c + ", start =" + I + ", end =" + temp);} str = str. substring (0, I-1); System. out. println ("[" + str + "]") ;}}

Http://java.662p.com/thread-447-1-1.html


Java intercepts a fixed-length string and then truncates the string once. How can this problem be achieved if the length is less than one?

Public static void main (String [] args ){
String ss = "aaaaaaaaaaaaa ";
Int n = 3;
String s = null;
Int t = 0;
Int a = ss. length ()/n + 1;
For (int I = 0; I <a; I ++ ){
If (ss. length ()> 0 ){
T = ss. length ()> n? N: ss. length ();
S = ss. substring (0, t );
Ss = ss. substring (t );
}
System. out. println (s );
}
}

Java string byte length Truncation

ContentSummanry = contentSummanry. substring (0,100); this means that I want to extract the first one hundred characters

This is the best part. You can try it.
ContentSummanry is the string you want to intercept here:
Public class CutString {

/**
* Determine whether it is a Chinese character
*
* @ Param c
* Characters
* @ Return true indicates Chinese characters, and false indicates English letters.
* @ Throws UnsupportedEncodingException
* The encoding format not supported by JAVA is used.
*/
Public static boolean isChineseChar (char c)
Throws UnsupportedEncodingException {
// If the number of bytes is greater than 1, it is a Chinese character
// The Difference Between English letters and Chinese characters in this way is not very rigorous, but in this question, this judgment is sufficient.
Return String. valueOf (c). getBytes ("GBK"). length> 1;
}

/**
* Truncate strings by byte
*
* @ Param orignal
* Original string
* @ Param count
* Number of truncated digits
* @ Return refers to the intercepted string.
* @ Throws UnsupportedEncodingException
* The encoding format not supported by JAVA is used.
*/
Public static String substring (String orignal, int count)
Throws UnsupportedEncodingException {
// The original character is neither null nor an empty string
If (orignal! = Null &&! "". Equals (orignal )){
// Convert the original string to GBK encoding format
Orignal = new String (orignal. getBytes (), "GBK ");
// The number of bytes to be truncated is greater than 0 and smaller than the number of bytes of the original string
If (count> 0 & count <orignal. getBytes ("GBK"). length ){
StringBuffer buff = new StringBuffer ();
Char c;... the remaining full text>

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.