字符串的截取、与整数的转换,字符串的基本判断
字符串的截取、与整数的转换,字符串的基本判断
public void testString() {
String str = "abABCef";
//截取字符串:
//1.1:从开始位置截取到尾:
String substring = str.substring(1);
System.out.println(substring);
//1.2: 指定开始位置和结束位置:(包含开头不包含结尾):
String substring1 = str.substring(1, 4);
System.out.println(substring1);
//判断是否是数字
//先转换为字符数组(toCharArray())
char[] chars1 = str.toCharArray();
boolean digit = Character.isDigit(chars1[0]);
//判断首字母是否大写:
char c = str.charAt(0);
boolean letter = Character.isLetter(c);
//1:返回字符串的长度
int length = str.length();
System.out.println(length);
// 2:返回字符串中指定索引出的值:
char a = str.charAt(3);
System.out.println("a="+a);
//3:将字符串转换为字符数组:
char[] chars = str.toCharArray();
for(int i=0;i<chars.length;i++){
char aChar = chars[i];
System.out.print(aChar+",");
}
//4:将字符串中的所有字符都转换为大写或则小写:
//大写:
String s = str.toUpperCase();
System.out.println("s="+s);
//小写:
String s1 = str.toLowerCase();
System.out.println("s1="+s1);
//5:去掉字符串最前面和最后面的空格:
String trim = str.trim();
System.out.println("trim="+trim);
//6:将字符串转换为整数类型:
String age = "24";
Integer ageInt = Integer.valueOf(age);
System.out.println("ageInt="+ageInt);
//将数字转换为字符串:
int age2 =32;
String s2 = String.valueOf(age2);
System.out.println("s2="+s2);
//7:比较两个字符串的值是否相等,不区分大小写:
String Str = "ab abcef";
boolean b = str.equalsIgnoreCase(Str);
System.out.println("b="+b);
//8:判断字符串是否以指定的前缀开始:
boolean ab = Str.startsWith("ab");
System.out.println(ab);
//9:判断字符串是否以指定的后缀结束:
boolean cef = Str.endsWith("cef");
System.out.println(cef);
//10:判断字符串是否包含子串:
boolean abc = Str.contains("abc");
System.out.println(abc);
}
}
@Test
public void testString(){
//截取指定字符到末尾或则任意位置的字符串:
String str = "abcdeabfg";
//包含c的下标:
String c = str.substring(str.indexOf("c"));
//不包含c的下标:
String c1 = str.substring(str.indexOf("c") + 1);
System.out.println("c="+c+"----"+"c1="+c1 );
//截取最后一次出现到末尾,或则任意下标的字符串:
int a = str.lastIndexOf("a");
String sb = str.substring(a);
System.out.println("sb="+sb);
}
@Test
public void testStr(){
//分割字符串
String str = "2546765805@qq.com";
String[] split = str.split("@");
System.out.println(Arrays.toString(split));
}
//分割字符串到集合中: public static void main(String[] args) { String str = "1,2,3,4,5"; List<String> strings = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(str); // 遍历集合 for(String st: strings){ System.out.println(st+"---"); } }