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.length();    //7 返回字符串的长度
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 place("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 dsWith("g");
System.out.println("endsWith( ):判断字符串的结束 "+a);
}
/
*6.字母⼤⼩写转换
* LowerCase();
* UpperCase();
*
7.字符串分割
str.split(String sign);
*/
//8.将字符串转化为字节数组!、getBytes();
public void getBytes(){
System.out.println();
String str="zhaou xu feng";
byte[] Bytes();
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 []CharArray();
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 ains("xu");
System.out.println("contains(xu):判断⼀个字符串中是否有另⼀个字符串 "+a); }
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。