string类型数组java_Java string类和数组的相关函数总结

一、string类

1、字符串查找

(1)str.indexOf(substr);//返回substr首次在str里出现的索引

str: 任意字符串对象

substr:要搜索的字符串

(2)str.lastIndexOf(substr);//返回substr最后一次在str里出现的索引

str:任意字符串

substr:要搜索的字符串

2、获取指定索引位置的字符

str.charAt(index);//返回索引是index的字符,index为int型

str:任意字符串对象

index:指定的索引

3、获取子字符串

(1)str.substring(beginIndex);//返回从beginIndex开始截取直到字符串结束的子串,beginIndex为int型

(2)str.substring(beginIndex, endIndex);//返回从beginIndex(包含)开始截取到endIndex(不包含)结束的子串

4、去除空格

str.trim()//返回字符串副本,忽略前导空格和尾部空格

eg: String str = " ab cd ";

String str1 = str.trim();//str1 = "ab cd";

5、字符串替换

str.replace(oldStr, newStr);//replace()会将所有的oldStr替换成newStr

oldStr:要替换的字符或字符串

newStr:用于替换原来字符串的新内容

6、判断字符串的开始与结尾

(1


版权声明:本文为weixin_29479879原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。