/**
* 判断传进来的字符串,是否
* 大于指定的字节,如果大于递归调用
* 直到小于指定字节数 ,一定要指定字符编码,因为各个系统字符编码都不一样,字节数也不一样
* @param s
* 原始字符串
* @param num
* 传进来指定字节数
* @return String 截取后的字符串
* @throws UnsupportedEncodingException
*/
public static String idgui(String s,int num)throws Exception{
int changdu = s.getBytes("UTF-8").length;
if(changdu > num){
s = s.substring(0, s.length() - 1);
s = idgui(s,num);
}
return s;
}
import java.io.UnsupportedEncodingException;
public class EncodeTest {
/**
* 打印字符串在指定编码下的字节数和编码名称到控制台
*
* @param s
* 字符串
* @param encodingName
* 编码格式
*/
public static void printByteLength(String s, String encodingName) {
System.out.print("字节数:");
try {
System.out.print(s.getBytes(encodingName).length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(";编码:" + encodingName);
}
public static void main(String[] args) {
String en = "A";
String ch = "人";
// 计算一个英文字母在各种编码下的字节数
System.out.println("英文字母:" + en);
EncodeTest.printByteLength(en, "GB2312");
EncodeTest.printByteLength(en, "GBK");
EncodeTest.printByteLength(en, "GB18030");
EncodeTest.printByteLength(en, "ISO-8859-1");
EncodeTest.printByteLength(en, "UTF-8");
EncodeTest.printByteLength(en, "UTF-16");
EncodeTest.printByteLength(en, "UTF-16BE");
EncodeTest.printByteLength(en, "UTF-16LE");
System.out.println();
// 计算一个中文汉字在各种编码下的字节数
System.out.println("中文汉字:" + ch);
EncodeTest.printByteLength(ch, "GB2312");
EncodeTest.printByteLength(ch, "GBK");
EncodeTest.printByteLength(ch, "GB18030");
EncodeTest.printByteLength(ch, "ISO-8859-1");
EncodeTest.printByteLength(ch, "UTF-8");
EncodeTest.printByteLength(ch, "UTF-16");
EncodeTest.printByteLength(ch, "UTF-16BE");
EncodeTest.printByteLength(ch, "UTF-16LE");
}
}
package com.newyulong.iptv.billing.ftpupload;
import java.io.UnsupportedEncodingException;
public class CutString {
/**
* 判断是否是一个中文汉字
*
* @param c
* 字符
* @return true表示是中文汉字,false表示是英文字母
* @throws UnsupportedEncodingException
* 使用了JAVA不支持的编码格式
*/
public static boolean isChineseChar(char c)
throws UnsupportedEncodingException {
// 如果字节数大于1,是汉字
// 以这种方式区别英文字母和中文汉字并不是十分严谨,但在这个题目中,这样判断已经足够了
return String.valueOf(c).getBytes("UTF-8").length > 1;
}
/**
* 按字节截取字符串
*
* @param orignal
* 原始字符串
* @param count
* 截取位数
* @return 截取后的字符串
* @throws UnsupportedEncodingException
* 使用了JAVA不支持的编码格式
*/
public static String substring(String orignal, int count)
throws UnsupportedEncodingException {
// 原始字符不为null,也不是空字符串
if (orignal != null && !"".equals(orignal)) {
// 将原始字符串转换为GBK编码格式
orignal = new String(orignal.getBytes(), "UTF-8");//
// System.out.println(orignal);
//System.out.println(orignal.getBytes().length);
// 要截取的字节数大于0,且小于原始字符串的字节数
if (count > 0 && count < orignal.getBytes("UTF-8").length) {
StringBuffer buff = new StringBuffer();
char c;
for (int i = 0; i < count; i++) {
System.out.println(count);
c = orignal.charAt(i);
buff.append(c);
if (CutString.isChineseChar(c)) {
// 遇到中文汉字,截取字节总数减1
--count;
}
}
// System.out.println(new String(buff.toString().getBytes("GBK"),"UTF-8"));
return new String(buff.toString().getBytes(),"UTF-8");
}
}
return orignal;
}
/**
* 按字节截取字符串
*
* @param orignal
* 原始字符串
* @param count
* 截取位数
* @return 截取后的字符串
* @throws UnsupportedEncodingException
* 使用了JAVA不支持的编码格式
*/
public static String gsubstring(String orignal, int count)
throws UnsupportedEncodingException {
// 原始字符不为null,也不是空字符串
if (orignal != null && !"".equals(orignal)) {
// 将原始字符串转换为GBK编码格式
orignal = new String(orignal.getBytes(), "GBK");
// 要截取的字节数大于0,且小于原始字符串的字节数
if (count > 0 && count < orignal.getBytes("GBK").length) {
StringBuffer buff = new StringBuffer();
char c;
for (int i = 0; i < count; i++) {
c = orignal.charAt(i);
buff.append(c);
if (CutString.isChineseChar(c)) {
// 遇到中文汉字,截取字节总数减1
--count;
}
}
return buff.toString();
}
}
return orignal;
}
/**
* 判断传进来的字符串,是否
* 大于指定的字节,如果大于递归调用
* 直到小于指定字节数
* @param s
* 原始字符串
* @param num
* 传进来指定字节数
* @return String 截取后的字符串
*/
public static String idgui(String s,int num){
int changdu = s.getBytes().length;
if(changdu > num){
s = s.substring(0, s.length() - 1);
s = idgui(s,num);
}
return s;
}
public static void main(String[] args) throws Exception{
// 原始字符串
String s = "我ZWR爱你们JAVA";
System.out.println("原始字符串:" + s + " : 字节数是: " + s.getBytes().length);
/* System.out.println("截取前1位:" + CutString.substring(s, 1));
System.out.println("截取前2位:" + CutString.substring(s, 2));
System.out.println("截取前4位:" + CutString.substring(s, 4)); */
//System.out.println("截取前12位:" + CutString.substring(s, 12));
System.out.println("截取前12字节:" + CutString.idgui(s, 11));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有