split() 方法根据匹配给定的正则表达式来拆分字符串。
注意: . 、 $、 | 和 * 等转义字符,必须得加 \。
注意:多个分隔符,可以用 | 作为连字符。
语法
public String[] split(String regex, int limit)
参数
regex – 正则表达式分隔符。
limit – 分割的份数。
返回值
字符串数组。
实例
public class Test {
public static void main(String args[]) {
String str = new String("Welcome-to-Runoob");
System.out.println("- 分隔符返回值 :" );
for (String retval: str.split("-")){
System.out.println(retval);
}
System.out.println("");
System.out.println("- 分隔符设置分割份数返回值 :" );
for (String retval: str.split("-", 2)){
System.out.println(retval);
}
System.out.println("");
String str2 = new String("www.runoob.com");
System.out.println("转义字符返回值 :" );
for (String retval: str2.split("\\.", 3)){
System.out.println(retval);
}
System.out.println("");
String str3 = new String("acount=? and uu =? or n=?");
System.out.println("多个分隔符返回值 :" );
for (String retval: str3.split("and|or")){
System.out.println(retval);
}
}
}
用 Scanner 实现字符串的输入有两种方法,一种是next(),一种nextLine();
next() 一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next() 方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符。
nextLine()方法的结束符只是Enter键。
简言之,next方法不能得到带空格的字符串,而nextLine()方法返回的是Enter键之前的所有字符
转自: https://blog.csdn.net/u011489043/article/details/65644104
toLowerCase() 方法将字符串转换为小写。
public class Test {
public static void main(String args[]) {
String Str = new String("WWW.RUNOOB.COM");
System.out.print("返回值 :" );
System.out.println( Str.toLowerCase() );
}
}
substring() 方法返回字符串的子字符串。
public class RunoobTest {
public static void main(String args[]) {
String Str = new String("This is text");
System.out.print("返回值 :" );
System.out.println(Str.substring(4) );
System.out.print("返回值 :" );
System.out.println(Str.substring(4, 10) );
}
}
以上程序执行结果为:
返回值 : is text
返回值 : is te
Java String contains() 方法
contains() 方法用于判断字符串中是否包含指定的字符或字符串。
实例
public class Main {
public static void main(String[] args) {
String myStr = "Runoob";
System.out.println(myStr.contains("Run"));
System.out.println(myStr.contains("o"));
System.out.println(myStr.contains("s"));
}
}
以上程序执行结果为:
true
true
false