碰到一个问题,对字符串按照byte位截取,然后把截取到的值copy到另外一个字符串。下面是我写的,但是并不能实现这个功能,比如length=8, 用substring会截到‘伟大的中华人民共’,但是我想要得结果是‘伟大的中’。如果s1 = '123456789'是半角字符,那么结果是'12345678',我该怎么实现呢?谢谢。
var length = 8;
var s1 = '伟大的中华人民共和国万岁';
var s2 = '';
var s3 = getByte(s1);
if (s2 == '')
{
s2.value = s1.substring(0,s3.length);
}
var length = 8;
var s1 = '伟大的中华人民共和国万岁';
var s2 = '';
var s3 = getByte(s1);
if (s2 == '')
{
s2.value = s1.substring(0,s3.length);
}
我用这个怎么报错啊
/**
* 构造方法
*/
private Stringor() {}
/**
* 得到字符串长度。可以是中英文混合字符串。中文字符长度为2。
*
* @param source 字符串
* @return 字符串长度
*/
public static int length(String source) { if (isNull(source)) {
source = "";
} try {
source = new String(getBytes(source), "ISO8859_1");
}
catch (java.io.IOException e) {
source = "";
} return source.length();
}
/**
* 得到字符串的字节数组。默认编码方式为 GBK
*
* @param source 字符串
* @return 字符串的字节数组
*/
public static byte[] getBytes(String source) { return getBytes(source, "GBK");
} /**
* 得到字符串的字节数组。
*
* @param source 字符串
* @param charCoding 编码方式
* @return 字符串的字节数组
*/
public static byte[] getBytes(String source, String charCoding) { if (source == null) {
return new byte[] {};
} if (charCoding == null) {
charCoding = "GBK";
} try {
return source.getBytes(charCoding);
}
catch (java.io.IOException e) {
return new byte[] {};
}
}
/**
* 截取指定长度的字符串
*
* @param source 字符串
* @param length 指定长度。每个中文字符按 2 计算。
* @return 截取后的字符串
*/
public static String substring(String source, int length) { return substring(source, 0, length, "");
} /**
* 截取指定长度的字符串
*
* @param source 字符串
* @param length 指定长度。每个中文字符按 2 计算。
* @param postfix 截断后字符串添加的后缀。默认为“...”。
* @return 截取后的字符串
*/
public static String substring(String source, int length, String postfix) { return substring(source, 0, length, postfix);
} /**
* 截取指定起始位置、指定长度的字符串
*
* @param source 字符串
* @param posStart 起始位置
* @param length 指定长度。每个中文字符按 2 计算。
* @return 截取后的字符串
*/
public static String substring(String source, int posStart, int length) { return substring(source, posStart, length, "");
} /**
* 截取指定起始位置、指定长度的字符串
*
* @param source 字符串
* @param posStart 起始位置
* @param length 指定长度。每个中文字符按 2 计算。
* @param postfix 截断后字符串添加的后缀。默认为“...”。
* @return 截取后的字符串
*/
public static String substring(String source, int posStart, int length,
String postfix) { if (posStart < 0) {
posStart = 0;
} if (length < 0) {
length = 0;
} if (postfix == null) {
postfix = "...";
} int strLength = length(source);
if (strLength <= 0) {
return "";
} int posEnd = posStart + length; if (posEnd > strLength) {
posEnd = strLength;
} byte[] bs = getBytes(source);
int i = 0;
for (i = posStart - 1; i >= 0; i--) {
if (bs[i] > 0) {
break;
}
}
if ((posStart - 1 - i) % 2 == 1) {
posStart = posStart + 1;
} for (i = posEnd - 1; i >= 0; i--) {
if (bs[i] > 0) {
break;
}
}
if ((posEnd - 1 - i) % 2 == 1) {
posEnd = posEnd + 1;
} source = new String(bs, posStart, posEnd - posStart); length = length(source);
if (length < strLength) {
source = source + postfix;
} return source;
}
}
祝你好运~
* 判断一个字符串是否为空
*
* @param aString 需要判断的字符串
* @return boolean true - 空;false - 非空
*/
public static boolean isNull(String aString) { return (aString == null);
}
结贴,送分。