把一段话的每个首字母大写,substring,spilt,length,length(),size(),substringspilt
分享于 点击 46034 次 点评:225
把一段话的每个首字母大写,substring,spilt,length,length(),size(),substringspilt
package test;
import java.util.Scanner;
public class LianXi {
//把一段话的每个首字母大写
public static void main(String[] args) {
System.out.println("Please input a english sentence:");
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
String[] lists = s.split(" ");
for (int i = 0; i < lists.length; i++) {
lists[i] = lists[i].substring(0, 1).toUpperCase() + lists[i].substring(1);
System.out.print(lists[i] + " ");
}
// LianXi.test(); //split方法limit参数测试
}
public static void test() {
String s = "boobooboo";
for (String a : s.split("o", 2)) { //匹配1次,分成2段
System.out.print("|_" + a + "_| ");
}
System.out.println();
for (String a : s.split("o", 8)) { //匹配7次,要分成8段,只有7段
System.out.print("|_" + a + "_| ");
}
System.out.println();
for (String a : s.split("o", 0)) { //匹配所有次,去除结尾空格
System.out.print("|_" + a + "_| ");
}
System.out.println();
for (String a : s.split("o", -2)) { //匹配所有次
System.out.print("|_" + a + "_| ");
}
System.out.println();
for (String a : s.split("o", -8)) { //匹配所有次
System.out.print("|_" + a + "_| ");
}
}
/*
|_b_| |_obooboo_|
|_b_| |__| |_b_| |__| |_b_| |__| |__|
|_b_| |__| |_b_| |__| |_b_|
|_b_| |__| |_b_| |__| |_b_| |__| |__|
|_b_| |__| |_b_| |__| |_b_| |__| |__|
*/
}
/*
String substring(int beginIndex)
Returns a string that is a substring of this string.
String substring(int beginIndex, int endIndex)
Returns a string that is a substring of this string.
String[] split(String regex)
Splits this string around matches of the given regular expression.
String[] split(String regex, int limit)
Splits this string around matches of the given regular expression.
limit 大于0 匹配 limit-1次,保证最后匹配
limit 等于0 无限次匹配,去除最后多余空格
limit 小于0 无限次匹配
*/
/*
length 数组对象使用 String[]
length() 字符串对象使用 String
size() 泛型集合对象使用 List
*/
相关文章
- 暂无相关文章
用户点评