1、trim()要领
trim() 要领用于删除字符串的头尾空缺符。
实例:
public class Test { public static void main(String args[]) { String Str = new String(" www.runoob.com "); System.out.print("原始值 :" ); System.out.println( Str ); System.out.print("删除头尾空缺 :" ); System.out.println( Str.trim() ); } }
效果:
原始值 : www.runoob.com 删除头尾空缺 :www.runoob.com
2、split()要领
split() 要领依据婚配给定的正则表达式来拆分字符串。
注重: . 、 | 和 * 等转义字符,必须得加 \\。
注重:多个分隔符,可以用 | 作为连字符。
语法:
public String[] split(String regex, int limit)
参数:
regex
-- 正则表达式分隔符。
limit
-- 支解的份数。
返回值:
字符串数组。
3、charAt()要领
charAt() 要领用于返回指定索引处的字符。索引局限为从 0 到 length() - 1。
语法:
public char charAt(int index)
参数:
index
-- 字符的索引。
返回值:
返回指定索引处的字符。
实例:
public class Test { public static void main(String args[]) { String s = "www.runoob.com"; char result = s.charAt(8); System.out.println(result); } }
效果:
o
4、substring()要领
substring() 要领返回字符串的子字符串。
语法:
public String substring(int beginIndex) 或 public String substring(int beginIndex, int endIndex)
参数:
beginIndex
-- 肇端索引(包含), 索引从 0 最先。
endIndex
-- 完毕索引(不包含)。
返回值
子字符串。
实例:
public class Test { public static void main(String args[]) { String Str = new String("www.runoob.com"); System.out.print("返回值 :" ); System.out.println(Str.substring(4) ); System.out.print("返回值 :" ); System.out.println(Str.substring(4, 10) ); } }
效果:
返回值 :runoob.com 返回值 :runoob
引荐教程:Java教程
以上就是java中的经常使用要领有哪些的细致内容,更多请关注ki4网别的相干文章!