一:字符串 String类和StringBuffer类
(1)初始化方法:
String s1 = “abs”
第二种是用String类的构造方法来申明:
String s1 = new String();
String s2 = new String("aa");
char[] c = {'a','b','c','d'};
String s3 = new String(c);
(2)String类的一些常用方法(共18种):
3int:
int indexOf(int ch)
int lastIndexOf(int ch)
int lenth()
2char:
char charAt(int index)
char[] toCharArray()
5boolean :
boolean endsWith(String suffix)
boolean equals(Object anObject)----String类覆盖了object类的equals方法
boolean isEmpty()
boolean startsWith(String prefix)
boolean contains(CharSequence cs)---是否包含指定的字符序列
8String
String toLowerCase()
String toUpperCase()
String valueOf(int i )---返回int参数的字符串表示形式
String replace(CharSequence oldstr, CharSequence newstr)---返回被替换之后新的字符串(替换所有)
String split(String regex)---分割
String substring(int beginIndex)---切割
String substring(int beginIndex,int endIndex)
String trim()---去空格
String的转换:
Example03.java:
public class Example03{
public void static main(String[] args){
String str = "absssss";
Char[] c = str.toCharArray();
for(int i = 0; i <= c.length -1 ; i ++ ){
if(i != c.length - 1 ) {
System.out.print(c[i] +",");
}else{
System.out.print(c[i]);
}
}
System.out.println(String.valueOf(11));
System.out.println(str.toUpperCase());
}
String的替换和去除空格操作:
public class Example05{
public void static main(String[] args){
String str = "itcast";
System.out.println(str.replace("it","cn.it");
String str1 = "i t c a s t "
System.out.println(str1.trim
);
System.out.println(str1.replace(" ","");
}
}
字符串的判断:
public class Example06{
public void static main(String[] args){
String s1 = "String";
String s2 = "Str";
System.out.println(s1.endsWith("ng"));
System.out.println(s1.startsWith("S"));
System.out.println(s1.contains("tri"));
System.out.println(s1.isEmpty());
System.out.println(s1.equals(s2));
}
}
字符串的截取和分割:
public class Example07{
public void static main(String[] args){
String s = "Peter-Morton-Ben";
System.out.println(s.substring(3));
System.out.println(s.substring(3,9));
String[] stringArray = s.split("-");
for(int i = 0; i < stringArray.length ; i ++){
if(i != (stringArray.length - 1)){
System.out.print(stringArray[i]+",");
}else{
System.out.print(stringArray[i]);
}
}
}
}
StringBuffer:
内容和长度可以改变
常用方法8个:
6StringBuffer:
StringBuffer append(char c)
StringBuffer insert(int offset , String str)
StringBuffer deleteCharAt(int index)
StringBuffer delete(int start, int end)
StringBuffer replace(int start,int end , String s)
StringBuffer reverse()---反转
1void:
void setCharAt(int index,char ch)
1String:
String toString()---返回StringBuffer缓冲区的字符串
都是自己在教室敲的,谢谢支持 |
|