1.charAt(index)的用法:根据index序号,获得在该字符串中的字符
eg:
String str = "this is mine";
for(int i = 0; i<str.length(); i++)
{
char a = str.charAt(i);
System.out.print(a);
}
输出效果:this is my String
2.toUpperCase():将字符串转大写
3.toLowerCase():将字符串转小写
4.substring(int beginIndex):指的是从指定的字符串位置开始,到最后结束
substring(int beginIndex, int endIndex):指的是从开始位置到结束位置,相当于(左闭右开区间)
5.replace():字符替换
eg:
String str = "this is mine";
str.replace('i','o'); //'o'将替换‘i’
6.replaceAll():替换整个字符串中的一部分字符串
eg:
str.replaceAll("this", "that"));
输出效果:that is mine
7.getChar(starPosition, endPosition, char数组,从该数组的哪个Position开始)
eg:
String str = "this is mine";
int srcBegin;
int srcEnd;
char[] dst = {'h','e','l','l','o'};
int dstBegin;
str.getChars(0, 3, dst, 0);
System.out.println(dst);
输出效果:
thilo
8.indexOf(“字符串”):获得第一次字符串的位置下标
indexOf(‘字符’):获得第一次该字符的下标
9.valueOf(‘字符’):获得该字符的值
valueOf(“字符串”):获得该字符的字符串
10.在字符串后面追加字符串
concat(String otherOne)
append(String otherOne)
eg:
str.concat("Hello");
String newOne = new StringBuffer("My name").append("is").append("ZB").append(10).append("years old").toString();
11.比较函数
oneString.compareTo(anotherString):将oneString与anotherStirng相减,返回的结果是一个差值数据
eg:
String a = "this";
int b = a.compareTo("thia");
System.out.println(b);
输出效果:-18
12.判断相等函数:
oneString.equals(anotherString),返回boolean类型
oneString == anotherString
equeals与“==”的区别:
“==”:
基本数据类型:比较的是值
引用数据类型:比较堆内存地址
“equals”:
不能用于基本数据类型比较
引用数据类型:比较堆内存地址