字符串常用方法

/*
字符串常用方法
*/


public class fuck3{

public static void main(String[] args){

//1 char charAt(int index);
//输出字符串指定下标的字符
String s1="我是曹柔莉,我是好人";
char c1=s1.charAt(2);
System.out.println(c1);

//2 boolean endsWith(String endstr);
//字符串1是不是以字符串2结尾
System.out.println("hello crl".endsWith("crl"));//true

//3 boolean equalsIgnorecase(String anotherString);
//忽略大小写字符串1,2是否相等
System.out.println("abc".endsWith("ABC"));//true

//4 byte[] getBytes[];
//创建一个字符数组接收字符串
byte[] bytes="abc".getBytes();

for(int i=0;i<bytes.length;i++){
System.out.println(bytes[i]);
}//abc

//5 int indexof(String str);
//输出字符串2在字符串1中的下标
System.out.println("hello crl".endsWith("crl"));//6

//6 int indexof(String str,int fromIndex);
//在字符串1中从x下标开始寻找字符串2

//7 int lastIndexof(String str);
//搜索字符串2在字符串1最后出现的下标

//8 int lastIndexof(String str,int fromIndex);
//在字符串1的x下标开始搜索字符串2

//9 int length();   数组是length属性,String是类型方法
//衡量字符串的长度

//10 String replaceAll(String s1,String s2);
//引用字符串里的所有字符串1用字符串2代替

//11 String[] spilt(String s);
//把引用字符串用字符串s进行分割之后的各部分保存在String类型数组里面

//12 boolean starWith(String s);
//判断字符串1是否以字符串2开始

//13 String substring(int begin);
//把字符串1从x下标开始截取

//14 String substring(int beginIndex,int endIndex);
//把字符串1从指定下标开始截取到截至下标的前一位
//包括前面不包括后面

//15 char[] toCharArray();
// 把字符串用一个字符数组进行接收

//16 toUpperCase(); 把字母转换成大写
//17 toLowerCaese(); 把字母转换成小写

//18 String trim();
//把字符串前面和后面的空格去掉

//String valueOf(Object o) 
//后面的括号可以加所有类型,最后给你转换成字符串类型
Object o=null;
System.out.println(o);
//不会空指针异常,实际上等于下式
//因为已经对o进行了处理
System.out.println(String.valueOf(0));

//会报空指针异常
System.out.println(o.toString());

}


}

猜你喜欢

转载自blog.csdn.net/rolic_/article/details/80278228