StringBuffer和StringBuilder比较
package com.string;
import org.junit.Test;
/**
* StringBuffer和StringBuilder比较:
* 不同点:
* 1. StringBuffer:是JDK1.0就有了,是线程安全的,效率较低
* 2. StringBuilder:是JDK1.5才有,是线程不安全的,效率较高
* 相同点:
* 1. 都有相同父类AbstractStringBuilder,也都实现Serializable, CharSequence接口
* 2. 它们都是可变的字符序列
* 3. 底层都是采用字符数组结构
* 4. 调用无参构造时是初始化长度为16的字符数组
* 5. 都可以操作字符串
*/
public class StringBufferStringBuilder {
@Test
public void test06() {
String str = "";
StringBuffer sb1 = new StringBuffer();
StringBuilder sb2 = new StringBuilder();
System.out.println("------String---------");
long start = System.currentTimeMillis();
for(int i=0; i<500000; i++) {
str += i;
}
long end = System.currentTimeMillis();
System.out.println(start - end);
System.out.println("------StringBuffer---------");
start = System.currentTimeMillis();
for(int i=0; i<500000; i++) {
sb1.append(String.valueOf(i));
}
end = System.currentTimeMillis();
System.out.println(start - end);
System.out.println("--------StringBuilder-------");
start = System.currentTimeMillis();
for(int i=0; i<500000; i++) {
sb2.append(String.valueOf(i));
}
end = System.currentTimeMillis();
System.out.println(start - end);
}
@Test
public void test05() {
StringBuilder sb = new StringBuilder();
sb.append("hello");
sb.append("world");
// insert(int offset, char str):在指定位置插入内容
sb.insert(5, ",");
System.out.println(sb);
// reverse():把StringBuilder对象中的内容反转
sb.reverse();
System.out.println(sb);
}
@Test
public void test04() {
StringBuilder sb = new StringBuilder("hello");
char charAt = sb.charAt(2);
System.out.println(charAt);
}
@Test
public void test03() {
StringBuilder sb = new StringBuilder("hello");
sb.append(",").append("world");
System.out.println(sb);
sb.delete(0, 5); // 删除StringBuilder对象中从指定位置到结束位置的内容
System.out.println("---" + sb);
sb = new StringBuilder("java,html,spring,springmvc,");
// 获取StringBuilder对象中的长度(此长度不是char数组长度,是实现存储内容的长度)
int length = sb.length();
System.out.println(sb);
sb.deleteCharAt(length - 1);
System.out.println(sb);
}
@Test
public void tese02() {
// char[] value = new char["hello".length() + 16];
StringBuilder sb = new StringBuilder("hello");
sb.append(",").append("world");
System.out.println(sb.toString());
}
@Test
public void test01() {
StringBuffer sb = new StringBuffer(); // char[] value = new char[16];
// append(char c)// 链式编程
sb.append("hello").append(",").append("world");
System.out.println(sb);
}
}
String类的使用
package com.string;
import org.junit.Test;
// String类的使用
public class StringTest {
// 当字符串使用的是常量进行拼接时,使用==比较会返回true
// 当比较的对象中包含有变量时,使用==比较会返回false
@Test
public void test02() {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = "hello" + "world";
String s5 = "hello" + s2;
String s6 = s1 + "world";
String s7 = s1 + s2;
final String s8 = "hello";
String s9 = s8 + "world";
System.out.println(s3 == s4); // true
System.out.println(s3 == s5); // false
System.out.println(s3 == s6); // false
System.out.println(s3 == s7); // false
System.out.println(s3 == s9); // true
System.out.println(s1 == s8);
}
@Test
public void test01() {
String str = "hello";
System.out.println(str);
String s = "hello";
System.out.println(str == s);
String sstr = str.substring(2);
System.out.println(sstr);
System.out.println(str);
// String类有两种创建方式:
// 第一种:
String string = "hello";
// 第二种:
String ss = new String("hello");
// char value = new char["hello".length()];
System.out.println(string == ss);
System.out.println(string.equals(ss));
}
}
String类的方法介绍
package com.string;
import java.util.Arrays;
import java.util.StringTokenizer;
import org.junit.Test;
/**
* String类的方法介绍
*/
public class StringMethodTest {
@Test
public void test07() {
String str = "hello,world,java,spring";
// split(String regex)
String[] split = str.split("\\s+");
System.out.println(Arrays.toString(split));
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
// split(String regex, int limit)
//String[] split2 = str.split("\\w+", 44);
//System.out.println(Arrays.toString(split2));
//System.out.println((int)',');
}
@Test
public void test06() {
String str = "hellohello";
// replace(char oldChar, char newChar)
String replace = str.replace('l', 'x');
System.out.println(replace);
// replace(CharSequence target, CharSequence replacement)
String replace2 = str.replace("el", "xx");
System.out.println(replace2);
// replaceAll(String regex, String replacement)
String replaceAll = str.replaceAll("l", "world");
System.out.println(replaceAll);
}
@Test
public void test05() {
String str = " hello ";
// trim():去掉字符串前后空格
String trim = str.trim();
System.out.println("a" + str + "b");
System.out.println("a" + trim + "b");
}
@Test
public void test04() {
String str = "endsWith";
// toLowerCase():把字符串的大写字母全部变为小写字母
String lowerCase = str.toLowerCase();
System.out.println(lowerCase);
// toUpperCase():把字符串的小写字母全部变为大写字母
String upperCase = str.toUpperCase();
System.out.println(upperCase);
}
@Test
public void test03() {
String str = "hello";
// endsWith(String suffix):判断字符串是否以指定字符串结尾
boolean endsWith = str.endsWith("ko");
System.out.println(endsWith);
// startsWith(String prefix):判断字符串是否以指定字符串开头
boolean startsWith = str.startsWith("h");
System.out.println(startsWith);
// substring(int beginIndex):从指定位置截取子字符串,包含指定位置
String substring = str.substring(2);
System.out.println(substring); // llo
// substring(int beginIndex, int endIndex):截取从指定beginIndex到endIndex位置对应子字符串,包含beginIndex,不包含endIndex
String substring2 = str.substring(1, str.length() - 1);
System.out.println(substring2); // ell
}
@Test
public void test02() {
String str = "hello";
//lastIndexOf(String str):从后向前查找
int index = str.lastIndexOf("l");
System.out.println(index);
// length():返回字符串的长度
int length = str.length();
System.out.println(length);
// compareTo(String anotherString):比较两个字符串
int compareTo = str.compareTo("apple");
System.out.println(compareTo);
// concat(String str):字符串连接
String s = str.concat("world");
System.out.println(s);
}
@Test
public void test01() {
String str = "hello";
// charAt(int index):返回指定索引的字符
char c = str.charAt(4);
System.out.println(c);
// indexOf(int ch):返回指定字符在字符串中第一次出现的位置
int index = str.indexOf(101);
System.out.println(index);
// indexOf(String ch):返回指定字符在字符串中第一次出现的位置
int i = str.indexOf("e");
System.out.println(i);
// indexOf(String str, int fromIndex):从指定位置查找指定字符串出现位置
index = str.indexOf("l", 4);
System.out.println(index);
}
}