-----------------------------------String类的常用方法

package string;

public class Test01 {

/**
* @param args
*/

public void test01(){
String str1="abcd";
String str2="ABCD";
System.out.println("字符串的长度:"+str1.length());
System.out.println("比较两个字符串的内容:"+str1.equals(str2));
System.out.println("比较两个字符串的内容:(忽略大小写)"+str1.equalsIgnoreCase(str2));
System.out.println("把str1变成大写:"+str1.toUpperCase());
System.out.println("把str2变成小写:"+str2.toLowerCase());

}

public void test02(){
String str1="abcd";
//把字符串转变成字符数组
char[] arrays = str1.toCharArray();
for (char c : arrays) {
System.out.println(c);
}

}


public void test03(){
String str1="a+b+c+d";
//^[0~9]{11}$ 正则表达式
//根据指定的规则 把字符串拆分成 字符串 数组
String[] split = str1.split("\\+");
for (String string : split) {
System.out.println(string);
}

}

public void test04(){
String str1="12345@qq.com";
//查询@在字符串出现的位置
int index = str1.indexOf("@");
System.out.println("@在字符串出现的位置"+index);//下标从0开始
//查询q在字符串中最后一次出现的位置
int lastIndex = str1.lastIndexOf("q");
System.out.println("q在字符串中最后一次出现的位置"+lastIndex);
//获取@之后的所有字符串
String sub = str1.substring(index+1);
System.out.println("@之后的所有字符串"+sub);
/***
* 查询 qq
* subString(int beginIndex, int endIndex)
* 01。找到起点的位置
* 02,不包含endIndex
*/
int index1 = str1.indexOf(".");
String sub1 = str1.substring(index+1, index1);
System.out.println(sub1);

}

public void test05(){
String str1=" abc def ";
System.out.println("空格计算长度:"+str1.length());
//去除字符串两边的空格
str1=str1.trim();
System.out.println("去除空格之后的内容:"+str1);
System.out.println("去除空格后字符串的长度:"+str1.length());

}
public void test06(){
String str1="abcdef";
//把a替换成6
String replace = str1.replace("a", "6");
System.out.println(replace);
//是否以6开始
System.out.println("是否以6开始"+replace.startsWith("6"));
System.out.println("是否以f结尾"+replace.endsWith("f"));
}

public void test07(){
String str1="abcdef";
//截取一个字符 返回值是char
char a =str1.charAt(3);
System.out.println(a);

}
public void test08(){
String str1="abcdef";
//将字符串贮存在数组中
byte[] bytes = str1.getBytes();
for (byte b : bytes) {
System.out.println((char)b);
}

}
public void test09(){

String str1="abc";
String str2="efg";
//之前字符串的相加
System.out.println(str1+str2);
//现在 concat(String str)
String concat = str1.concat(str2);
System.out.println(concat);

}
public void test10(){
String str1="abc";
//是否包含摸个字符
boolean contains = str1.contains("a");
System.out.println(contains);

}

/*
* String, StringBuffer ,StringBuilder三者的区别
* 01.在执行速度
* StringBuilder > StringBuffer > String
* 02.StringBuilder:多线程不安全,适合单线程
* StringBuffer:线程安全
* String : 数据量小的时候使用
* 03.String 是不可变的! 每当我们操作字符串的时候,都会创建一个新的对象!
* StringBuilder,StringBuffer当我们操作字符串的时候,实际上是在操作一个对象!
*/
public void test11(){
//01.定义常量
String name="xiaohei";
//02.定义操作字符串的次数
int count=2000000;
//03.设置开始时间
long beginTime = System.currentTimeMillis();
for (int i = 0; i <count/100; i++) {
name+="haha";
}
//03.设置结束时间
long endTime = System.currentTimeMillis();
System.out.println("String的执行时间"+(endTime-beginTime));
System.out.println("***************");
//使用StringBuffer的时间
name="xiaohei";
StringBuffer buffer=new StringBuffer(name);
beginTime=System.currentTimeMillis();
for (int i = 0; i < count; i++) {
StringBuffer append = buffer.append("haha");

}
endTime=System.currentTimeMillis();
System.out.println("使用StringBuffer的执行时间:"+(endTime-beginTime));
System.out.println("***************");
//使用StringBuilder的时间
name ="xiaohei";

StringBuilder builder=new StringBuilder(name);
beginTime=System.currentTimeMillis();
for (int i = 0; i <count; i++) {
builder.append("haha");
}
endTime = System.currentTimeMillis();
System.out.println("使用StringBuilder的执行时间"+(endTime-beginTime));
}


}

posted @ 2017-05-15 11:08  花落知到啥  阅读(148)  评论(0编辑  收藏  举报