**
* 输入一个字符串和字节数,输出为按字节截取的字条符串,但要保证汉字不被截半
* @author Administrator
*
*/
public class Ceshi {
public static void main(String[] args) {
String str = "sfsfs中国sdfsdfsfd";
System.out.println(substring(str, 10));
}
private static String substring(String str, int interceptLength){
StringBuilder sb = new StringBuilder();
for(int i=0; i<interceptLength; i++){
sb.append(str.charAt(i));
}
return sb.toString();
}
}
-------------------------------------------------------------------
import java.util.StringTokenizer;
/** *//**
public class SplitTest {
public static void main(String[] args) {
SplitTest st = new SplitTest();
// st.splitTest01();
//st.splitTest02();
st.StringTokenizierTest();
}
public void splitTest01() {
String s = "01111:aa:哈哈";
/**//*
* limit 该值用来限制返回数组中的元素个数
*/
String[] s1 = s.split(":");
System.out.println(s1[0]);
/**//*
* -1表示如果为空也能区分出来
*/
String[] s2 = s.split(":", -1);
System.out.println(s2[0]);
String[] s3 = s.split(":", 1);
System.out.println(s3[0]);
}
public void splitTest02() {
// “.”和“|”都是转义字符,必须得加"\\"
String s = "bulktree.oakertree.laoshulin";
String ss = "bulktree|oakertree|laoshulin";
String sss = "bulktree.oakertree|laoshulin";
String[] s1 = s.split("\\.");
System.out.println(s1[0]);
String[] s2 = ss.split("\\|");
System.out.println(s2[0]);
/**//*
* 如果有多个分隔符,可以用"|"作为连字符
*/
String[] s3 = sss.split("\\.|\\|");
System.out.println(s3[1]);
}
/**//*
* 构造字符串的StringTokenizer对象测试
*/
public void StringTokenizierTest() {
String s = "I am a student,bulktree";
StringTokenizer str = new StringTokenizer(s);
// 返回nextToken方法被调用的次数,分隔符的数量
System.out.println(str.countTokens());
// 是否有分隔符
while(str.hasMoreTokens()) {
System.out.println(str.nextToken());
}
}
}
* 输入一个字符串和字节数,输出为按字节截取的字条符串,但要保证汉字不被截半
* @author Administrator
*
*/
public class Ceshi {
public static void main(String[] args) {
String str = "sfsfs中国sdfsdfsfd";
System.out.println(substring(str, 10));
}
private static String substring(String str, int interceptLength){
StringBuilder sb = new StringBuilder();
for(int i=0; i<interceptLength; i++){
sb.append(str.charAt(i));
}
return sb.toString();
}
}
-------------------------------------------------------------------
import java.util.StringTokenizer;
/** *//**
public class SplitTest {
public static void main(String[] args) {
SplitTest st = new SplitTest();
// st.splitTest01();
//st.splitTest02();
st.StringTokenizierTest();
}
public void splitTest01() {
String s = "01111:aa:哈哈";
/**//*
* limit 该值用来限制返回数组中的元素个数
*/
String[] s1 = s.split(":");
System.out.println(s1[0]);
/**//*
* -1表示如果为空也能区分出来
*/
String[] s2 = s.split(":", -1);
System.out.println(s2[0]);
String[] s3 = s.split(":", 1);
System.out.println(s3[0]);
}
public void splitTest02() {
// “.”和“|”都是转义字符,必须得加"\\"
String s = "bulktree.oakertree.laoshulin";
String ss = "bulktree|oakertree|laoshulin";
String sss = "bulktree.oakertree|laoshulin";
String[] s1 = s.split("\\.");
System.out.println(s1[0]);
String[] s2 = ss.split("\\|");
System.out.println(s2[0]);
/**//*
* 如果有多个分隔符,可以用"|"作为连字符
*/
String[] s3 = sss.split("\\.|\\|");
System.out.println(s3[1]);
}
/**//*
* 构造字符串的StringTokenizer对象测试
*/
public void StringTokenizierTest() {
String s = "I am a student,bulktree";
StringTokenizer str = new StringTokenizer(s);
// 返回nextToken方法被调用的次数,分隔符的数量
System.out.println(str.countTokens());
// 是否有分隔符
while(str.hasMoreTokens()) {
System.out.println(str.nextToken());
}
}
}