String类的成员函数
substring:从下标a(取),到下标b(不取)
String str="we are students and he is a techer";
System.out.println(str.substring(2,10));
equals:返回布尔值
str1.equals(str2)
equalsIgnoreCase():忽略字符大小写的字符串比较
str1.equalsIgnoreCase(str2)
字符串查找
public class test {
public static void main(String[] args) {
String str="我们一起数到6吧!";
System.out.println(str.indexOf("一"));
System.out.println(str.indexOf("6"));
System.out.println(str.startsWith("我"));
System.out.println(str.endsWith("!"));
}
}
输出结果:
2
6
true
true
遍历字符串的每一位字符(String类不能直接取下标)
String s="abcde";
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
System.out.print(c+" ");//输出a b c d e,获取字符串
}
String[] s1={"a","b","c","d","e"};
for(int i=0;i<s1.length;i++)
{
System.out.print(s1[i]+" ");//输出a b c d e,获取字符串数组
}
indexof函数返回下标(找不到返回-1) startwith endwith 返回boolean
str.indexOf(','),表示在整个str字符串中检索!
int n3 = str.indexOf(',',n1);表示从n1开始(n1也算)检索!
split函数(返回String数组)
public static void main(String[] args) {
String str="good good study, day day up";
String[] strarray=str.split(" ");
for (int i = 0; i < strarray.length; i++)
System.out.println(strarray[i]);
}
String类静态函数
format按格式串构造数组
System.out.println(String.format("%02d:%02d:%02d",hh,mm,ss));
StringBuffer
public class Str {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer("good");
sb.append(" study");
System.out.println(sb);
sb.reverse();
System.out.println(sb);
sb.delete(1,3);
System.out.println(sb);
sb.insert(3, "hello");
System.out.println(sb);
sb.insert(0, "11");
System.out.println(sb);
sb.replace(1, 2, "hello");
System.out.println(sb);
}
}
输出结果:
good study
yduts doog
yts doog
ytshello doog
11ytshello doog
1helloytshello doog
本文作者:林动