- int length() : 返回字符串的长度: return value.length
- char charAt(int index) : 返回某索引处的字符 return value[index]
- boolean isEmpty() : 判断是否是空字符串: return value.length == 0
- String toLowerCase() : 使用默认语言环境,将 String 中的所有字符转换为小写String toUpperCase() : 使用默认语言环境,将 String 中的所有字符转换为大写String trim() : 返回字符串的副本,忽略前导空白和尾部空白
- boolean equals(Object obj) : 比较字符串的内容是否相同
- boolean equalsIgnoreCase(String anotherString) : 与 equals方法类似,忽略大小写
- String concat(String str) : 将指定字符串连接到此字符串的结尾。等价于用“+”int compareTo(String anotherString) : 比较两个字符串的大小
- String substring(int beginIndex) : 返回一个新的字符串,它是此字符串的从beginIndex 开始截取到最后的一个子字符串。
- String substring(int beginindex, int endIndex) : 返回一个新字符串,它是此字符串从 beginIndex 开始截取到 endIndex (不包含)的一个子字符串。
- boolean endsWith(String suffix) : 测试此字符串是否以指定的后缀结束boolean startsWith(String prefix) : 测试此字符串是否以指定的前缀开始
- boolean startsWith(String prefix,int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始
- boolean contains(CharSequence s) : 当且仅当此字符串包含指定的char值序列时,返回 true
- int indexOf(String str) : 返回指定子字符串在此字符串中第一次出现处的索引
- int indexOf(String str, int fromindex) : 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
- int lastIndexOf(String str) : 返回指定子字符串在此字符串中最右边出现处的索引
- int lastIndexOf(String str, int fromindex):返回指定子字符串在此字符串中最后次出现处的索引,从指定的索引开始反向搜索
注: indexOf 和 lastIndexOf 方法如果未找到都是返回-1
package com.klvchen.java;
import org.junit.Test;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import java.util.Locale;
public class myTest {
@Test
public void test4(){
String str1 = "中国教育出版社";
String str2 = str1.replace("中国", "广州");
System.out.println(str1); //中国教育出版社
System.out.println(str2); //广州教育出版社
System.out.println("***********************************");
String str = "12hello34world5java7891mysql3456";
String string = str.replaceAll("\d+", ",").replaceAll("^,|,$", "");
System.out.println(string); //hello,world,java,mysql
str = "12345";
// 判断str字符串中是否全部由数字组成,即有1-n个数字组成
boolean matches = str.matches("\d+");
System.out.println(matches); //true
//判断这是否是一个杭州的固定电话
String tel = "0571-12345678";
boolean result = tel.matches("0571-\d{7,8}");
System.out.println(result); //true
System.out.println("***********************************");
str = "hello|world|java";
String[] strs = str.split("\|");
for (int i = 0; i < strs.length; i++){
System.out.println(strs[i]);
}
System.out.println();
str2 = "hello.world.java";
String[] strs2 = str2.split("\.");
for (int i = 0; i < strs2.length; i++ ){
System.out.println(strs2[i]);
}
}
@Test
public void test3(){
String str1 = "helloworld";
boolean b1 = str1.endsWith("ld");
System.out.println(b1); //true
boolean b2 = str1.startsWith("He");
System.out.println(b2); //false
boolean b3 = str1.startsWith("ll", 2);
System.out.println(b3); //true
String str2 = "wo";
System.out.println(str1.contains(str2)); //true
System.out.println(str1.indexOf("lol")); //-1
System.out.println(str1.indexOf("lo",5)); //-1
String str3 = "helloworld";
System.out.println(str3.lastIndexOf("or")); //6
System.out.println(str3.lastIndexOf("or", 6)); //6
}
@Test
public void test2(){
String s1 = "HelloWorld";
String s2 = "helloworld";
System.out.println(s1.equals(s2)); //false
System.out.println(s1.equalsIgnoreCase(s2)); //true
String s3 = "abc";
String s4 = s3.concat("def");
System.out.println(s4); //abcdef
String s5 = "abc";
String s6 = new String("abe");
System.out.println(s5.compareTo(s6)); // -2, 涉及字符串排序
String s7 = "中国教育出版社";
String s8 = s7.substring(2);
System.out.println(s7); // 中国教育出版社
System.out.println(s8); // 教育出版社
}
@Test
public void test1() {
String s1 = "hellowoRld";
System.out.println(s1.length()); //10
System.out.println(s1.charAt(0)); //h
System.out.println(s1.charAt(9)); //d
System.out.println(s1.isEmpty()); //false
String ss = "";
System.out.println(ss.isEmpty()); //true
String s2 = s1.toLowerCase();
System.out.println(s1); //hellowoRld
System.out.println(s2); //helloworld
String s3 = " he llo world ";
String s4 = s3.trim();
System.out.println("----------" + s3 + "------------"); //---------- he llo world ------------
System.out.println("----------" + s4 + "------------"); //----------he llo world------------
}
}