public class StringDemo {
private static final long LEVEL = 20150701;//码讲版本
/*
* String 基本操作
*/
@Test
public void stringtest1() throws UnsupportedEncodingException {
String s1 = "123abc";
String s2 = "123abc";
String s3 = "123";
String s4 = s3 + "abc";
String s5 = "123" + "abc";
String s6 = 1 + "23" + "abc";
String s7 = 1 + '2' + "3abc";
String s8 = new String("123abc");
System.out.println(s1 == s2);// true String直接量会使用缓存
System.out.println(s1 == s4);// false 只要有变量参与就新建对象存
System.out.println(s1 == s5);// true 编译器会将直接量计算后在运行
System.out.println(s1 == s6);// true
System.out.println(s1 == s7);// false 1+‘2’将为数字
System.out.println(s1 == s8);// false
System.out.println(s1.equals(s8));// true
System.out.println("length:" + s1.length());
// java字母和汉字占的字节数与才用的编码方式有关
System.out.println("j".getBytes("UTF-8").length);// 1
System.out.println("好".getBytes("UTF-8").length);// 3
}
/*
* indexOf lastIndesOf
*/
public void stringTest3() {
String str = "thinking in java";
int index = str.indexOf("java");
int index1 = str.indexOf("in", 3);
int index2 = str.lastIndexOf("in");
System.out.println(index + "overload" + index1 + "last" + index2);// 12overload5last9
}
// 找出所有字串的位置
public void findStr() {
String str = "**java***java*****java*";
String str1 = "java";
int index = 0;
while ((index = str.indexOf(str1, index)) != -1) {
System.out.print(index + "");
index += str1.length();// 减少查找,提高速度
}
}
/*
* substring
*/
public void stringTest4() {
String str = "thinking in java";
System.out.println(str.substring(4, 8));// king
System.out.println(str.substring(9));// in java
}
public void stringTest5() {
String url = "http://www.baidu.com";
int start = url.indexOf(".") + 1;
int end = url.indexOf(".", start);
// int end = url.lastIndexOf(".");
String str = url.substring(start, end);
System.out.println(str);
}
/*
* trim
*/
public void stringTest6() {
String str = " \t lksjelk" + "\n";
System.out.println(str);
System.out.println(str.trim());
}
/*
* charAt
*/
public void testCharAt() {
String str = "Thinking in Java";
System.out.println(str.charAt(6));
}
/*
* startWith endsWith
*/
public void testStart() {
String str = "Thinking in Java";
System.out.println(str.startsWith("Thi"));// true
System.out.println(str.endsWith("va"));// true
}
/*
* toLowerCase toUpperCase
*/
public void testLowerAndUpper() {
String str = "jslfjAFFKDJ";
System.out.println(str.toLowerCase());
System.out.println(str.toUpperCase());
}
/*
* valueOf
*/
// @Test
public void testValueOf() {
// if .class will return class name
String str = String.valueOf(new TestToString());//TestToString在最后面
System.out.println(str);
}
/*
* 判断一个字符串是否为回文
*/
public void isHuiwen() {
String str = "上海自来水来自海上";
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
System.out.println("是回文");
return;
}
}
System.out.println("不是回文");
}
/*
* 输出所有汉字
*/
public void printCh() {
StringBuilder builder = new StringBuilder();
for (char i = '\u4e00', sum = 1; i <= '\u9fa5'; i++, sum++) {
builder.append(i);
if (sum % 50 == 0) {
builder.append("\n");
}
}
System.out.println(builder.toString());
}
// ++++++++++++++++++++++++==正则表达式+++++++++++++++++++=
/*
* match
*/
public void testMatch() {
// 匹配email java默认进行全匹配
String regex = "\\w+@\\w+(\\.[a-zA-Z]+)+";
String email = "fancq@slkjfl.com.cn";
if (email.matches(regex)) {
System.out.println("This is a email!");
} else {
System.out.println("This can't be a email");
}
}
/**
* split图片重命名
*/
public void testSplit() {
// ,,,123,123,,,123,,,-->[, , , 123, 123, , , , 43, 545, 435]
String regex = "\\.";
String str = "123.jpg";
String[] strs = str.split(regex);
str = System.currentTimeMillis() + "." + strs[1];
System.out.println(str);
}
/**
* replaceAll用于和谐聊天用语
*/
public void testReplaceAll() {
String str = "skj234glskej34234jlj3lk4234lkj2l3kj4lk3";
str = str.replaceAll("\\d+", "#");
System.out.println(str);
}
/*
* StringBuilder replace delete insert reverse
*/
public void testStringBuilder() {
StringBuilder sb = new StringBuilder("this is a test string");
System.out.println("原始字符:" + sb);
sb.replace(15, sb.length(), "StringBuilder");
System.out.println("replace后字符:" + sb.toString());
sb.delete(0, 4);
System.out.println("delete后字符:" + sb.toString());
sb.insert(0, "that");
System.out.println("insert后字符:" + sb.toString());
sb.reverse();
System.out.println("reverse后字符:" + sb.toString());
}
}
class TestToString {
@Override
public String toString() {
return "toString called";
}
}