* String类常用操作:
* 拼接(+ concat)
* 查找 indexOf lastIndexOf contains
* 替换 replace
* 比较字符串内容 equals,contentEquals
* 判断是否符合正则表达式:matches
* 提取部分内容:substring
* 格式化输出 format
<h6>构建字符串对象:</h6>@Test
public void test1() throws UnsupportedEncodingException{
String s = "abc";//引用字符串缓冲池中的对象
String s1 = new String("abc");
byte[] content ="探险队".getBytes("gbk");
String s2 = new String(content,"utf-8");
System.out.println(s2);//会乱码
String s3 = new String(s.getBytes());
System.out.println(s3); //abc
char[] c={'a','b','c'};
String s4 = new String(c);
}
<h6>倒序输出</h6>@Test
public void test2(){
//获取字符串长度
//获取指定位置字符
//以倒序的形式将用户输入的字符串输出
String s = "我是一个中国人";
for (int i = s.length(); i >=0; i--) {
System.out.println(s.charAt(i));
}
}
<h6>判断用户输入的是中文</h6>@Test
public void test3(){//获取unicode代码点
String s = "我是一个中国人";
//System.out.println(s.codePointAt(0));
// System.out.println(s.codePointAt(7));
//判断用户输入的都是中文 \u4e00---\u9fa5
boolean c = true;
for (int i = 0; i < s.length(); i++) {
if(s.codePointAt(i)<'\u4e00'||s.codePointAt(i)>'\u9fa5'){
c=false;
break;
}
}
System.out.println(c);
}
<h6>字符串比较(字典顺序)</h6>@Test
public void test4(){
String s ="abc";
String s2 ="acd";
System.out.println(s.compareTo(s2)); // -1
s ="abc";
s2 ="bcd";
System.out.println(s.compareTo(s2)); //-1
s ="abc";
s2 ="Acd";
System.out.println(s.compareTo(s2)); //32
System.out.println(s.compareToIgnoreCase(s2)); //-1
}
<h6>用concat进行字符串拼接</h6>@Test
public void test5(){
String a ="abc";
String b = "def";
String c = a.concat(b);//字符串拼接,产生一个新的字符串对象
System.out.println(a);//字符串是不可变对象,不管对String对象做何操作,这个对象内容不会改变
System.out.println(c);
}
<h6>是否包含指定字符串/contentequals</h6>@Test
public void test6(){
String s = "jzm575444";
System.out.println(s.contains("jzm"));//true
String s2 = new String("jzm");
System.out.println(s.contentEquals(s2));
}
<h6>equals(Object o) return true/false on any type of data, depend if the content is equal !
contentEquals(CharacterSequence cs) returns true if and only if this String represents the same sequence of characters as the specifiedStringBuffer.</h6>
@Test
public void testx(){
String str1 = "Hello";
String str2 = new String("Hello");
StringBuilder str3 = new StringBuilder(str1);
System.out.println(str1.equals(str2));//true
System.out.println(str1.contentEquals(str2));//true
System.out.println(str1.equals(str3));//false
System.out.println(str1.contentEquals(str3));//true
}
<h6>比较字符串相等</h6>@Test
public void test7(){
String uname = "张飞";
String pwd = "123";
Scanner input = new Scanner(System.in);
String name = input.nextLine();
String p = input.nextLine();
if(uname.equals(name)&&pwd.equals(p)){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
}
@Test
public void test8(){
String s = "abc";
String s2 = "abc";//字符串常量放在缓冲池,同一个字符串常量在池中只有一个
String s3 = new String("abc");
//==用来比较变量的值,引用对象的变量存储的是引用的对象的地址
//如果两个引用类型变量的值相等,说明他们引用同一个对象
System.out.println(s==s2);//true
System.out.println(s==s3);//false
System.out.println(s2==s3);//false
//equals用来比较对象是否相等
System.out.println(s2.equals(s3));//true
}
@Test
public void test9(){
String s = "a"+"b"+"c";//这个表达式中每个都是常量,字符串常量优化(编译时优化)
String s2 = "abc";
System.out.println(s==s2);//true
}
@Test
public void test10(){
String s = "a";
String s2 = s +"b";//其中有变量,不会编译时优化
String s3 = "ab";
System.out.println(s2==s3);//false
}
<h6>字符串格式化输出</h6>
@Test
public void test11(){
//格式:张三的成绩是89,体重是100.5
/*String s = String.format("%s的成绩是%d,体重是%.2f", "张三",89,100.5f);
System.out.println(s);
Calendar c = Calendar.getInstance();//日历对象,可以进行日历计算
System.out.println(c.get(Calendar.YEAR));
String cs = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
System.out.println(cs);*/
String s =String.format("%2$6.3f,字符串是%s",5f,6f,"abc");//
System.out.println(s);
System.out.println(s.length());
}
<h6>字符串出现处的索引</h6> @Test
public void test12(){
String s = "aabbacc";
System.out.println(s.indexOf("b"));//2 这个字符串第一次出现的索引
System.out.println(s.indexOf("ba"));//3 这个字符串第一次出现的索引
System.out.println(s.indexOf("bc"));//-1 未找到返回-1
System.out.println(s.lastIndexOf('c'));//最后一次出现的索引
System.out.println(s.indexOf('a',3)); //a第三次出现的索引
}
<h6>字符串替换</h6>@Test
public void test14(){
String s = "aabbabac";
//a替换为d
String s2 = s.replace('a', 'd');
System.out.println(s);
System.out.println(s2);
String s3 = s.replace("ab","ww");
System.out.println(s3);
s = "aaa";
s =s.replace("aa", "bb");//bba
System.out.println(s);
}
<h6>字符串拆分</h6>@Test
public void test15(){
String s = "a b c d";
String[] c = s.split(" ");
for (String st : c) {
System.out.println(st);
}
s="张三89李四77王五98";
c = s.split("\\d{1,}");
for (String st : c) {
System.out.println(st);
}
}
<h6>提取字符串/trim()</h6>@Test
public void test16(){
String s ="haha welcome you";
//s.replace("welcome","");
//提取5-8的字符
System.out.println(s.substring(5,8));
System.out.println(s.substring(5,9));
//包括开始索引不包括结束索引
System.out.println(s.substring(5));
//trim()删除字符串前后的空白
s =" abc ";
System.out.println(s.length());
System.out.println(s.trim().length());
//
}
<h6> * String是一个不可变字符串对象,对String对象执行任何操作都会产生一个新的字符串对象
*如果字符串的内容在程序运行时才能动态确定,使用StringBuffer或者Stringbuffer
*StringBuffer 是线程安全的可变字符序列 效率低 可以使用在多线程环境
*StringBuilder 是线程不安全的可变字符序列 效率高 只适合使用在单线程</h6> @Test
public void test(){
StringBuffer sb = new StringBuffer();
sb.append("我是中国人");
System.out.println(sb.toString());
sb.append(true);
System.out.println(sb.toString());
sb.append(10);//末尾插入
System.out.println(sb.toString());
sb.insert(0, "haga");//指定位置插入
System.out.println(sb.toString());
}
@Test
public void test2(){
//要求接受用户输入的一个字符串,将它以倒序的方式输出
String s = "abcdefg";
StringBuffer sb = new StringBuffer(s);
s = sb.reverse().toString();
System.out.println(s);
String sql ="";
sql+="select id,name from users";
sql +="where sex=’男‘";
sql += "name like '张'";
//7个对象
String s1 = "";
for (int i = 0; i < Integer.MAX_VALUE; i++) {
s1+="";
}
}