Java String常用方法

1、字符串查询

charAt(int index) 
		查找字符串某一位置字符。

indexOf(String str) 
		查找某一字符或子字符串在该字符串中的从左边起首次出现的位置,并返回。
indexOf(String str,int fromIndex) 
		查找某一字符或子字符串在该字符串中的从fromIndex起首次出现的位置,并返回。   
		
lastIndexOf(String str) 
		与indexOf的区别是从末尾开始查找。

lastIndexOf(String str, int fromIndex) 
		与lastIndexOf类似,从指定的索引fromIndex开始进行反向搜索。
    
contains(CharSequence s)   (也包括空格,例如:xxx.contains(" - "),用于分解字符串十分棒!)
		判断字符串是否包含指定的s值,有则返回 true

2、字符串连接

concat(String str)
		字符串连接,将str字符串连接到当前字符串后面,相当于“+”。

3、字符串拆分

split("")
		字符串拆分,根据双引号里的内容进行拆分。
		
		例如:
		String[] time= datatime.split(" - ");
		根据“ - ”进行拆分,并分别存进time数组里;

4、字符串比较

endsWith(String suffix) 
          判断此字符串是否以指定的后缀结束。

startsWith(String prefix) 
          判断此字符串是否以指定的前缀开始。
          
compareTo(String anotherString)
		该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0compareTolgnore(String anotherString)
		与compareTo方法相似,但忽略大小写。
		
equals(Object anObject) 
		比较两个字符串的内容,注意不能为nullequalsIgnoreCase(String anotherString) 
		与equals方法相似,但忽略大小写。

regionMatches(boolean b, int firstStart, String other , int otherStart, int length)
		从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。

5、字符串转换为数组

split(String regex) 
		以regex为分割符,将String字符串转换为String数组。

toCharArray()String字符串转换为char字符数组。
          		
format(Locale l, String format, Object... args) 
		格式字符串和参数返回一个格式化字符串。

getBytes(String charset) 
		将string转换为byte 数组。

getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 
		将string转换为字符数组

6、字符串替换

matches(String regex) 
		判断此字符串是否匹配给定的正则表达式。

replace(char oldChar, char newChar) 
		将所有 oldChar 替换成newChar,并返回一个新的字符串。

replaceFirst(String regex, String replacement)
		该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,并返回新的字符串。

replaceAll(String regex,String replacement)/该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,并返回新的字符串。

7、StringBuffer类常用方法

StringBuffer(String str) 
		构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。

append(基础类型数据 b) 
		将 基础类型的字符串表示形式追加到序列后面。


delete(int start, int end) 
		 移除此序列的子字符串中的字符。

deleteCharAt(int index) 
		移除此序列指定位置的 charinsert(int offset, 基础类型数据 b) 
		将 基础类型参数的字符串表示形式插入指定位置中。

toString() 
		返回此序列中数据的字符串表示形式。

8、截取list集合

subList(int firstIndex, int endIndex)
		根据下标,将集合从firstIndex截取到endIndex。

例如 List<StudentPO> pageList = list.subList(0,3);
		将list集合中从第0条截取到第3条,再赋值给pageList。

9、其他

valueOf(基础数据类型 b) 
		将基础类型数据的文本转换为字符串

substring(int beginIndex) 
		该方法从beginIndex位置截取剩余字符串并返回
		
substring(int beginIndex,int endIndex)      
		该方法从beginIndex位置截取到endIndex并返回
   
trim() 
		去除字符串两边的空格,中间不做处理。

isEmpty() 
		当且仅当 length()0 时返回 truetoLowerCase()String 中的所有字符都转换为小写。
          
toUpperCase()String 中的所有字符都转换为大写。

你可能感兴趣的:(Java,spring)