java 基础 记录 String 字符串

.

java中是没有字符串类型,字符串是通过java中的类库预定义字符串类实现的,这个类叫String

 

 

String 实际就是一个 char数组

String 类中的常用方法

1.char charAt(int index)//返回在指定位置的字符

2.int compareTo(String anotherString)

String str= "1";

String str1 ="2";

扫描二维码关注公众号,回复: 4626368 查看本文章

int compareTo=str.compareTo(str1 );

// 将 str 和str1 进行比较 比较是 按照 Unicode 字符集顺序。如果str1 (Unicode 字符集顺序)在 此 str的后面返回整数,             反之  为负整数,相等于零,因为str 在 str1 前面,所以 compare 等于 整数 

3. boolean endsWith (String suffix)//判断此字符串是否给指定的 ,包含后面部分(按照 字符串倒序:

比如 123 判断 123 为true , 23 为true  , 3 为true,32 为false)  是 true 否 false

4. boolean startsWith (String suffix)//与 3相反

5. BooleanequalsIgnoreCase(StringanotherString)//忽略大小写 判断值是否相等

6.int  indexof(String str)// 反回指定字符串 第一次出现位置 ,如果没有该字符串 返回 -1

7.int  indexof(String str,int fromIndex)// fromIndex 指定从什么位置开始寻找( 反回指定字符串 第一次出现位置 ,如果没             有该字符串 返回 -1)

8.int  lastIndexOf(String str))//

9.int  lastIndexOf(String str,int fromIndex))//   fromIndex指定位置 (反过来 从最后开始查找  和 第6个一样)

10 int length()//返回字符串长度

11 String replace(char oldChar , char newChar) //替换字符串 生成新的字符串   oldChar需要替换的字符串 ,newChar 替             换成什么字符串。

12 String toLowerCase()//将字符串变成小写 ,生成新的字符串

13 String toLowerCase()// 12 反之 

14 String trim() //去掉前后空格 生成新的字符串

 

猜你喜欢

转载自blog.csdn.net/xianailili/article/details/83591337