Intercepting string Java code by byte

Source: Internet
Author: User

Intercepting string Java code by byte
This article provides three Java intercept string functions, they can not take the byte to intercept the string length of code, very convenient example.
* Take the first tocount character of the string
*
* @param str is processed string
* @param tocount Intercept length
* @param more suffix string
* @version 2004.11.24
* @author Zhulx
* @return String
*/

Public
static string substring (string str, int tocount,string more)
{
int reint = 0;
String restr = "";
if (str = null)
Return "";
char[] Tempchar = Str.tochararray ();
for (int kk = 0; (KK < tempchar.length && tocount > Reint); kk++) {
string S1 = str.valueof (Tempchar[kk]);
Byte[] B = s1.getbytes ();
Reint + = B.length;
Restr + = Tempchar[kk];
}
if (Tocount = = Reint | | (Tocount = = reint-1))
Restr + +;
return restr;
}


Intercept String Method Two

Import java.util.*;

public class Study {
public static void Main (string[] args) {
StringBuilder sb = new StringBuilder ();
System.out.println (System.currenttimemillis ());
for (int i = 0; i < 1000000; i++) {
Sb.append ("1234567890");
}
System.out.println (System.currenttimemillis ());
String str = sb.tostring ();
Random rand = new Random ();
for (int i = 0; i < 1000000; i++) {
int beg = Rand.nextint (5000000);
int end = Rand.nextint (5000000) +beg;
Str.substring (Beg, end);
}
System.out.println (System.currenttimemillis ());
}
}

Method Three

Public static string substring (string s, int length) throws exception
{
byte[] bytes = S.getb Ytes ("Unicode");
int n = 0;//representing the current number of bytes
int i = 2;//number of bytes to intercept, starting at 3rd byte

for (; i < bytes.length && N < Leng Th i++) {
//odd positions, such as 3, 5, 7, for the second byte of two bytes in the UCS2 encoding
if (i% 2 = = 1) {
n++;//in UCS2 second byte n plus 1
}
else{
// When the first byte of the UCS2 encoding is not equal to 0 o'clock, the ucs2 character is Chinese, and a Chinese character is counted as two bytes
if (Bytes[i]!= 0) {
n++;
}
}
}
//If I is odd, process an even
if (i% 2 = 1)
{
///The UCS2 character is kanji, remove this truncated Chinese character
if (bytes[i-1 ]!= 0) {
i = i-1;
}
else{//The UCS2 character is a letter or a number, leave the character
i = i + 1;
}
}
return new string (bytes, 0, I, Unicode);
}

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.