String 常见的十种方法!

  

public class ZiFuChuan {
public static void main(String[] args) {


ZiFuChuanFangFa f=new ZiFuChuanFangFa();
f.IndexOf();    //1.字符串查找 注意空格在数组中的位置!字符串查找 indexOf(); 找不到就返回 -1 有就返回此元素在该数组中的角标位置
f.chartAt();    //2.获取指定位置的字符
f.substring01(); //3.获取子字符串!
f.substring02(); //在字符串中 截取一部分 有头无尾!
f.startWith();    //4.判断字符串的开头和结尾!
f.endsWith();
f.getBytes();    //5.将字符串转化为字节 数组!、getBytes();
f.toCharArray();//6.将字符串 转字符 数组!
f.length();    //7 返回字符串的长度
f.contains();    //8.判断一个字符串中是否有另一个字符串?
f.replace();    //9..字符串替换 可以实现将指定的字符串 和字符 换成新的玩意!oldChar: 要替换的字符或字符串 newChar: 用于替换原来的字符串内容!
f.equals(); //10.判断两个字符串对象 是否相等!    
}

}
class ZiFuChuanFangFa{

private static final char oldChar = 0;
public void IndexOf()
{
String str="Ni Hao ";
int a=str.indexOf("p");
int a1=str.indexOf("i");
System.out.println("indexOf(‘p'):查找字符串数组中是否有 p 元素 没有就返回 "+a);
System.out.println("indexOf(‘o'):查找字符串数组中是否有 o 元素 有就返回此元素在该数组中的角标位置 "+a1);


}
public void equals() {    //10.在Java中 判断两个对象是否相等 不能“==”表示 会报错 需要 调用equal()方法!--->比较的字符串的内容是否相等
String str= new String("123");    // 演示 对错!
String str01= new String("123");
boolean a=str.equals(str01);
System.out.println(str ==str01);
System.out.println(a);

}
//2.获取指定索引位置的字符 在计算机 会直接将 字符转化成字节 a--->97
public void chartAt(){
System.out.println();
String str="zhaou xu feng";
int a=str.charAt(2);
System.out.println("chartAt(2):获取字符串中角标为2的元素! "+a);

}
//3.获取子字符串 开头到所有!
public void substring01(){
System.out.println();
String str="zhou xu feng";
String a=str.substring(8);    
System.out.println("sunstring(2): 获取字符串中角标为2的元素 ,并取2以后的所有元素 生成一个新的字符串(子字符串!包含的关系) "+a);

}
//3.1 获取字符串 区间性 有头无尾! int beginIndex,int endImdex
public void substring02(/*int beginIndex,int endImdex*/){
System.out.println();
String str="zhou xu feng";
String a=str.substring(1, 4);    //可以在 字符串中截取一部分
System.out.println("substring(1,4: 在字符串中截取一部分 有头无尾!) "+a);

}
//4.字符串替换 可以实现将指定的字符串 和字符 换成新的玩意!oldChar: 要替换的字符或字符串 newChar: 用于替换原来的字符串内容!
public void replace(){
System.out.println();
String str="zhou xu feng";    //oldChar: 要替换的字符或字符串
String a=str.replace("feng", "hui");    //newChar: 用于替换原来的字符串内容!
System.out.println("replace(qq,cc):字符串替换 后面的替换前面的 "+a);
}
//5.判断字符串的开始和结尾
public void startWith(){
System.out.println(); 
String str="zhaou xu feng";
boolean a=str.startsWith(str);
System.out.println("startsWith(str):判断字符串的开始 "+a);
}
public void endsWith( ){
System.out.println();
String str="zhaou xu feng";
boolean a=str.endsWith("g");
System.out.println("endsWith( ):判断字符串的结束 "+a);

}
/*6.字母大小写转换
* str.toLowerCase();
* str.toUpperCase();
* 
7.字符串分割
str.split(String sign);
*/
//8.将字符串转化为字节数组!、getBytes();
public void getBytes(){
System.out.println();
String str="zhaou xu feng";
byte[] arr=str.getBytes();
System.out.println("getBytes():将字符串转为字节数组!");
for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i]+" ");
}
}
//9.将字符串 转字符数组!
public void toCharArray()
{
System.out.println();
System.out.println();
String str="zhaou xu feng";
System.out.println("getCharArray():将字符串转为字符组!");
char []arr=str.toCharArray();
for (int i = 0; i < arr.length; i++) 
{
System.out.print(arr[i]);
}

}
//10 返回字符串的长度
public void length(){
System.out.println();
String str="zhaou xu feng";
int a= str.length();
System.out.println("length():返回字符串的长度!"+a);
}
//11.判断一个字符串中是否有另一个字符串?
public void contains(){
String str="zhaou xu feng";
boolean a=str.contains("xu");
System.out.println("contains(xu):判断一个字符串中是否有另一个字符串 "+a);
}



}
原文地址:https://www.cnblogs.com/ZXF6/p/10555219.html