중국어를 포함하는 문자열의 처음 N 자 캡처
22330 단어 문자열
package com.wangzhu.string;
import java.io.UnsupportedEncodingException;
public class SubStringDemo1 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = " j abc";
String charset = "UTF-8";
printGetStrByLen(str, charset);
charset = "GBK";
printGetStrByLen(str, charset);
str = " ABC d";
charset = "UTF-8";
printGetStrByLen(str, charset);
charset = "GBK";
printGetStrByLen(str, charset);
// j abc==== ===UTF-8
// j abc====0
// 3===0
//
//
// j abc====1
// 3===0
//
//
// j abc====2
// 3===0
//
//
// j abc====3
// 3===1
//
//
// j abc====4
// 6===1
//
//
// j abc====5
// 6===1
//
//
// j abc====6
// 6===2
//
//
// j abc====7
// 7===3
// j
//
// j abc====8
// 10===3
// j
//
// j abc====9
// 10===3
// j
//
// j abc====10
// 10===4
// j
//
// j abc====11
// 11===5
// j a
//
// j abc====12
// 12===6
// j ab
//
// j abc====13
// j abc
//
// j abc====14
// j abc
//
// -------------
// j abc==== ===GBK
// j abc====0
// 2===0
//
//
// j abc====1
// 2===0
//
//
// j abc====2
// 2===1
//
//
// j abc====3
// 4===1
//
//
// j abc====4
// 4===2
//
//
// j abc====5
// 5===3
// j
//
// j abc====6
// 7===3
// j
//
// j abc====7
// 7===4
// j
//
// j abc====8
// 8===5
// j a
//
// j abc====9
// 9===6
// j ab
//
// j abc====10
// j abc
//
// j abc====11
// j abc
//
// -------------
// ABC d==== ===UTF-8
// ABC d====0
// 3===0
//
//
// ABC d====1
// 3===0
//
//
// ABC d====2
// 3===0
//
//
// ABC d====3
// 3===1
//
//
// ABC d====4
// 4===2
// A
//
// ABC d====5
// 5===3
// AB
//
// ABC d====6
// 6===4
// ABC
//
// ABC d====7
// 9===4
// ABC
//
// ABC d====8
// 9===4
// ABC
//
// ABC d====9
// 9===5
// ABC
//
// ABC d====10
// 12===5
// ABC
//
// ABC d====11
// 12===5
// ABC
//
// ABC d====12
// 12===6
// ABC
//
// ABC d====13
// ABC d
//
// ABC d====14
// ABC d
//
// -------------
// ABC d==== ===GBK
// ABC d====0
// 2===0
//
//
// ABC d====1
// 2===0
//
//
// ABC d====2
// 2===1
//
//
// ABC d====3
// 3===2
// A
//
// ABC d====4
// 4===3
// AB
//
// ABC d====5
// 5===4
// ABC
//
// ABC d====6
// 7===4
// ABC
//
// ABC d====7
// 7===5
// ABC
//
// ABC d====8
// 9===5
// ABC
//
// ABC d====9
// 9===6
// ABC
//
// ABC d====10
// ABC d
//
// ABC d====11
// ABC d
//
// -------------
}
public static void printGetStrByLen(String str, String charset)
throws UnsupportedEncodingException {
System.out.println(str + "==== ===" + charset);
for (int i = 0, len = str.getBytes(charset).length + 2; i < len; i++) {
System.out.println(getStrByLen(str, i, charset, false));
System.out.println();
}
System.out.println("-------------");
}
/**
* 【UTF-8:3 byte,GBK:2 byte】
*
* @param str
* @param len
* @param charset
* @param flag
* @return
* @throws UnsupportedEncodingException
*/
public static String getStrByLen(String str, int len, String charset,
boolean flag) throws UnsupportedEncodingException {
System.out.println(str + "====" + len);
byte[] buf = str.getBytes(charset);
int step = 1;
if (charset.equals("UTF-8")) {
step = 2;
}
int length = buf.length;
if (len >= length) {
return str;
}
int count = 0, size = 0;
for (int i = 0; i < length; i++) {
count++;
if (buf[i] < 0) {
//
i += step;
count += step;
}
size++;
if (count >= len) {
if (!flag) {
if ((count > len) && (buf[i] < 0)) {
size--;
}
}
System.out.println(count + "===" + size);
return str.substring(0, size);
}
}
return null;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
비슷한 이름의 Attribute를 많이 만들어 삭제하는 Houdini사용 소프트웨어는 Houdini16.5입니다 배열에서는 애트리뷰트의 보간이 잘 동작하지 않는 것과 AttributeCreateSOP 노드에서 Size가 4를 넘는 애트리뷰트를 작성해도 값이 조작할 수 없어 의미가 없...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.