字符串的操作String

亿阳信通的面试(一面)

String 底层是char[]数组

1.字符串截取

string.substring(int beginIndex)

从索引位置截取直到结尾

String str="hello";
String str1=str.substring(1);
system.out.println(str1);//ello

substring(beginIndex,endIndex)

String str="hello";
String str1=str.substring(1,4);
system.out.println(str1);//ell

2.字符串查找

indexOf("str")

String str="hello"
int size=indexOf("s");//无这个值返回-1,
int size=indexOf("e");//返回下标1;因为String底层是char[]

3.字符串替换replace()

String str="hello";
system.out.println(str.replace('e','a'));//hallo

4.字符串的长度

String str="hello";
System.out.println(str.length());//5

5.判断字符串是否相等

String str="hello";
String str2="hel"+"lo";
System.out.println(str.equals(str2));//true

equals()判断字符串的值相等;==判断字符串的存储空间地址

原文地址:https://www.cnblogs.com/Darius-Bennett/p/7701455.html