-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
public class StringTest {
public static void main(String [] args){
String s = "abdfdladhjfnv";
System.out.println(s.charAt(3));
System.out.println(s.indexOf('f'));
System.out.println(s.lastIndexOf('f'));
System.out.println(s.length());
System.out.println(s.substring(5));
}
}
public class StringTest {
public static void main(String [] args){
String s = "test.java";
System.out.println(s.contains("ja"));
System.out.println(s.endsWith("java"));
System.out.println(s.isEmpty());
}
}
public class StringTest {
public static void main(String [] args){
char [] chs = new char [] {'a','b','c','d'};
System.out.println(new String(chs));//将字符数组转成字符串。
System.out.println(String.valueOf(7));//将数字转成字符串
}
}
运行结果:
abcd
7
public class StringTest {
public static void main(String [] args){
StringBuffer sb = new StringBuffer();
sb.append("abc");
sb.append(7);
sb.append(3.0d);//添加任意元素
System.out.println(sb.toString());
sb.delete(4,sb.length());
sb.replace(0, 2, "java");//将角标0-2(不包含尾)替换成Java
System.out.println(sb.toString());
System.out.println(sb.reverse());//反转字符串
}
}
//匹配 手机号段只有 13xxx 15xxx 18xxxx
public class MatchesTest {
public static void main(String [] args){
String str ="15626677782";
boolean flag = matches(str);
if(flag==true)
System.out.println("手机号正确");
else
System.out.println("手机号错误");
}
public static boolean matches(String s){
String regex ="1[358]\\d{9}";
return s.matches(regex); //字符串匹配正则
}
}
import java.util.Arrays;
public class MatchesTest {
public static void main(String [] args){
String str ="abbbcdffeeghs";
System.out.println(Arrays.toString(split(str)));
}
public static String[] split(String s){
String regex ="(.)\\1+"; //定义切割的规则
return s.split(regex); //按照规则切割字符串。
}
}
public class MatchesTest {
public static void main(String [] args){
String str ="abbbcdffeeghs";
System.out.println(replaceAll(str));
}
public static String replaceAll(String s){
return s.replaceAll("(.)\\1+","$1"); //按照定义好的规则替换内容。
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchesTest {
public static void main(String [] args){
String str ="ming tian jiu yao fang jia le ,da jia";
get(str);
}
public static void get(String s){
String regex = "\\b[a-z]{3}\\b";
Pattern p = Pattern.compile(regex);//将规则封装成对象
Matcher m = p.matcher(s); //正则对象与要作用的字符串关联,并获取匹配器对象
while(m.find())
System.out.println(m.group());
}
}
-----------android培训、java培训、java学习型技术博客、期待与您交流!------------