String基本用法
string基本函数
package day4;
import java.util.Arrays;
public class StringDemo {
public static void main(String[] args)throws Exception{
String str0=new String();//""
System.out.println(str0);
char []ch={'c','b','n','v','g'};
String str1=new String(ch);//字符数组转字符串
System.out.println(str1);
String str2=new String(ch,0,2);//2是个数
System.out.println(str2);
String str3=new String("abc");
byte bt0[]=str3.getBytes();
System.out.println(Arrays.toString(bt0));
String str4=new String("你好");
byte bt1[]=str4.getBytes("utf-8");//中文是三个字节
System.out.println(Arrays.toString(bt1));
//解码
String str5=new String(bt1,"utf-8");
System.out.println(str5);
//--------------------------------------------------
String str6=new String("你好吗");
byte bt2[]=str6.getBytes("utf-8");
System.out.println(Arrays.toString(bt2));
String str7=new String(bt2,0,3,"utf-8");//utf-8编码中文是3个字节
System.out.println(str7);
}
}
package day4;
import java.util.Arrays;
public class StringDemo {
public static void main(String[] args)throws Exception{
/* String str0=new String();//""
System.out.println(str0);
char []ch={'c','b','n','v','g'};
String str1=new String(ch);//字符数组转字符串
System.out.println(str1);
String str2=new String(ch,0,2);//2是个数
System.out.println(str2);
String str3=new String("abc");
byte bt0[]=str3.getBytes();
System.out.println(Arrays.toString(bt0));
String str4=new String("你好");
byte bt1[]=str4.getBytes("utf-8");//中文是三个字节
System.out.println(Arrays.toString(bt1));
//解码
String str5=new String(bt1,"utf-8");
System.out.println(str5);
//--------------------------------------------------
String str6=new String("你好吗");
byte bt2[]=str6.getBytes("utf-8");
System.out.println(Arrays.toString(bt2));
String str7=new String(bt2,0,3,"utf-8");//utf-8编码中文是3个字节
System.out.println(str7);
*/
String s0="abc";
String s1="abc";
System.out.println(s0==s1);//true 同一个对象
System.out.println(s0.equals(s1));//true 值相等
String s2=new String("def");
String s3=new String("def");
System.out.println(s2==s3);//false 不同一个对象
System.out.println(s2.equals(s3));//true 值相等
String str="abcjdbasncsnjns";
System.out.println(str.length());//长度
System.out.println(str.indexOf("c"));//c首次出现的索引值 2
System.out.println(str.indexOf("b",str.indexOf("c")));// 5
System.out.println(str.contains("csd"));//false
System.out.println(str.concat("fff"));//连接
System.out.println(str.charAt(1));//b
char ch[]=str.toCharArray();
System.out.println(Arrays.toString(ch));
System.out.println(str.startsWith("abc"));
System.out.println(str.endsWith("ins"));
System.out.println(str.lastIndexOf("s"));
String str3="abc";
String str4="ABC";
System.out.println(str3.equalsIgnoreCase(str4));
System.out.println("a".compareTo("b"));//97-98=-1
System.out.println(str.isEmpty());//不为空
System.out.println(str.substring(3));//jdbasncsnjns
System.out.println(str.substring(1,5));//不包括5 bcjd
System.out.println(str.toUpperCase());//转大写
System.out.println(String.valueOf(12));//转化为字符串
String str5=new String("abcdfeghijklmn");
String str6="a b c d e f g";
String sx[]=str6.split(" ");
System.out.println(Arrays.toString(sx));
String str7="ca:bf:cfr:dgff:rtf:dee:gd";
String sxx[]=str7.split(":");
System.out.println(Arrays.toString(sxx));
System.out.println(str7.replace(":",""));
String str8=" sn jb ";
System.out.println(str8.length());//9
System.out.println(str8.trim().length());//6 只是去掉了首尾的字符串的空格
}
}
正则表达式
package day4;
import java.util.Arrays;
public class RegxDemo {
public static void main(String[] args) {
/*String s="1";
boolean b=s.matches("\\d");//判断一次 指的是判断只一位数是否为数字。true
System.out.println(b);
s="123456";
b=s.matches("\\d+");//一次或多次 true
System.out.println(b);
s="123";
b=s.matches("\\d*");//零次或多次
System.out.println(b);
s="123";
b=s.matches("\\d?");//一次或一次也没有
System.out.println(b);
s="4";
b=s.matches("[4-9]");//判断一位数是否在4-9之间
System.out.println(b);
s="456";
b=s.matches("[456]\\d+");
//大陆手机号码
s="18220413336";
System.out.println(s.matches("[1][35789]\\d{9}"));
//qq号码的判断
s="2213489623";
System.out.println(s.matches("[1-9]\\d{5,9}"));
String s1="anc1cda2dfasd5";
String str[]=s1.split("\\d");
System.out.println(Arrays.toString(str));
String s2="bjb8nvd8vndnw232";
System.out.println(s2.replace("8",""));
System.out.println(s2.replaceAll("\\d","qq"));
*/
String s0="adv_552678jckdh";
boolean d;
System.out.println(s0.matches("\\w+"));//true判断的是一次或多次的 数字字母下划线
s0="aghrfy";
System.out.println(s0.matches("[A-z]*"));
s0="";
System.out.println(s0.matches("[0-9]*"));//true0次或多次
//邮箱的判断
String st="221458687@qq.com";
System.out.println(st.matches("\\w{8,10}@\\w{2,5}.(com|com.cn)"));
String st1="221458387@sina.com";
System.out.println(st.matches("\\w{8,10}@\\w{2,5}\\.(com|com\\.cn)"));
String st2="123123123123123";
System.out.println(st2.matches("[1-3]*"));//true
}
}
StringBuffer 与StringBuilder
String:是字符常量,适用于少量的字符串操作的情况。
StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况。
StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况。
package day4;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();//默认长度为16
sb.append("cnsj");
sb.append(23);
System.out.println(sb);//cnsj23
sb.insert(1,"cnbs");
System.out.println(sb);//ccnbsnsj23
sb.replace(1,3,"fhbejg");
System.out.println(sb);//cfhbejgbsnsj23
System.out.println(sb.delete(1,3));//cbejgbsnsj23
System.out.println(sb.deleteCharAt(2));
System.out.println(sb.reverse());
System.out.println(sb.substring(1,5));//2jsn
}
}