package com.njupt.demo;
import java.util.Arrays;
public class Demo2 {
public static void main(String[] args) {
// String str="傻蛋";
// System.out.println(str);
String str = "我爱刘亦菲";
String str1 = "abcde";
//获得字符串的长度
System.out.println(str.length());
System.out.println(str1.length());
//获取特定位置上的字符
System.out.println(str.charAt(1));
//获取特定字符的索引
System.out.println(str.indexOf('爱'));
//替换特定的字符或字符串
String str2 = str.replace("刘亦菲", "刘诗诗");
System.out.println(str2);
System.out.println(str);
//判断两个字符串是否相等
String str3 = "liushishi";
String str4 = "liuyifei";
String str5 = "liushishi ";
//trim()去掉字符串两边的空格
String str6 = str5.trim();
System.out.println(str3.equals(str4));
System.out.println(str3.equals(str5));
System.out.println(str3.equals(str6));
String str7 = " 我爱 刘亦菲 ";
String str8 = str7.trim();
System.out.println(str8);
String str9 = str7.replace(" ","");
System.out.println(str9);
//将所有的字符串都变成大写
String str10 = str5.toUpperCase();
System.out.println(str10);
//将所有的字符串变成小写
String str11 = "LIUYIFEI";
System.out.println(str11);
System.out.println(str11.toLowerCase());
//判断字符串是否以某一个字符开头
System.out.println(str11.startsWith("L"));
//判断字符串是否已某一个字符结尾
System.out.println(str11.endsWith("I234"));
//字符串的截取
String str12 = "iloveyou";
String str13 = str12.substring(1,6); //截取[1,6)区间的字符串
System.out.println(str13);
//substring(1) 默认截取到最后一个元素
String str14 = "iloveyou";
String str15 = str14.substring(1);
System.out.println(str15);
String str16 = "i.love.liu.yi.fei";
String[] str17 = str16.split(",");
//以下一行代码的输出值是:Ljava.lang.String;@11e78461
//因为String类里面的toString()方法默认的实现就是包名+类名@哈希值
System.out.println(str17);
//要输出字符串数组的内容,这是得调用Arrays.toString()方法
System.out.println(Arrays.toString(str17));
//split(rex,limit);rex是分隔符。limit是分隔后所得的字符串数组的长度。
//即从左到右以rex作为分隔符进行分隔,分隔后得到的字符串数组有limit个元素
String[] str18 = str16.split(".",2);
System.out.println( "打印字符数组的长度:"+str18.length);
System.out.println( "把字符串额内容打印出来:"+Arrays.toString(str18));
String str19 = "foo:foo1:foo2:foo3:foo4";
System.out.println(Arrays.toString(str19.split(":",3)));
//假如使用.作为分隔符则一定要使用\\进行转义。以为.是转义字符
//否则结果与预期的不一样
String str20 = "foo.foo1.foo2.foo3.foo4";
System.out.println(Arrays.toString(str20.split("\\.",2)));
//|作为分隔符时,也要使用\\进行转义,否则会得不到想要的结果,
//结果为:[, f, oo|foo1|foo2|foo3|foo4]
String str22 = "foo|foo1|foo2|foo3|foo4";
System.out.println(Arrays.toString(str22.split("|",3)));
//|做为分隔符的正确用法str22.split("\\|",3)
//其运行结果为:[foo, foo1, foo2|foo3|foo4]
String str23 = "foo|foo1|foo2|foo3|foo4";
System.out.println(Arrays.toString(str23.split("\\|",3)));
//当使用多个分隔符时可以这样写str.split(分隔符1|分隔符2)
//以下代码的输出结果为:[acount=? , uu =? , n=?]
String str21 = "acount=? and uu =? or n=?";
System.out.println(Arrays.toString(str21.split("and|or")));
//判断字符传里面是否包含某个值
String str24 = "我爱刘亦菲";
System.out.println(str24.contains("爱"));//true
System.out.println(str24.contains("爱刘"));//true
System.out.println(str24.contains("我刘"));//false
System.out.println("--------------------------");
String str25 = "我爱刘亦菲 ";
String str26 = "我爱刘亦菲";
boolean ok = str25.contentEquals(str26);
System.out.println(ok);
String str27 = new String("我爱刘亦菲 ");
String str28 = new String("我爱刘亦菲");
Foo foo1 = new Foo();
Foo foo2 = new Foo();
//对于下面的一个东西,有些人看了可能会有疑问。为什么会是true呢?equals不是比较地址值吗?。
//因为sun在开发String时就重写了equals方法,使它比较的是内容而不再是地址
System.out.println(str27.equals(str28));//true
System.out.println(foo1.equals(foo2));//false
System.out.println(str27.contentEquals(str28));//true
}
}
class Foo{
}
import java.util.Arrays;
public class Demo2 {
public static void main(String[] args) {
// String str="傻蛋";
// System.out.println(str);
String str = "我爱刘亦菲";
String str1 = "abcde";
//获得字符串的长度
System.out.println(str.length());
System.out.println(str1.length());
//获取特定位置上的字符
System.out.println(str.charAt(1));
//获取特定字符的索引
System.out.println(str.indexOf('爱'));
//替换特定的字符或字符串
String str2 = str.replace("刘亦菲", "刘诗诗");
System.out.println(str2);
System.out.println(str);
//判断两个字符串是否相等
String str3 = "liushishi";
String str4 = "liuyifei";
String str5 = "liushishi ";
//trim()去掉字符串两边的空格
String str6 = str5.trim();
System.out.println(str3.equals(str4));
System.out.println(str3.equals(str5));
System.out.println(str3.equals(str6));
String str7 = " 我爱 刘亦菲 ";
String str8 = str7.trim();
System.out.println(str8);
String str9 = str7.replace(" ","");
System.out.println(str9);
//将所有的字符串都变成大写
String str10 = str5.toUpperCase();
System.out.println(str10);
//将所有的字符串变成小写
String str11 = "LIUYIFEI";
System.out.println(str11);
System.out.println(str11.toLowerCase());
//判断字符串是否以某一个字符开头
System.out.println(str11.startsWith("L"));
//判断字符串是否已某一个字符结尾
System.out.println(str11.endsWith("I234"));
//字符串的截取
String str12 = "iloveyou";
String str13 = str12.substring(1,6); //截取[1,6)区间的字符串
System.out.println(str13);
//substring(1) 默认截取到最后一个元素
String str14 = "iloveyou";
String str15 = str14.substring(1);
System.out.println(str15);
String str16 = "i.love.liu.yi.fei";
String[] str17 = str16.split(",");
//以下一行代码的输出值是:Ljava.lang.String;@11e78461
//因为String类里面的toString()方法默认的实现就是包名+类名@哈希值
System.out.println(str17);
//要输出字符串数组的内容,这是得调用Arrays.toString()方法
System.out.println(Arrays.toString(str17));
//split(rex,limit);rex是分隔符。limit是分隔后所得的字符串数组的长度。
//即从左到右以rex作为分隔符进行分隔,分隔后得到的字符串数组有limit个元素
String[] str18 = str16.split(".",2);
System.out.println( "打印字符数组的长度:"+str18.length);
System.out.println( "把字符串额内容打印出来:"+Arrays.toString(str18));
String str19 = "foo:foo1:foo2:foo3:foo4";
System.out.println(Arrays.toString(str19.split(":",3)));
//假如使用.作为分隔符则一定要使用\\进行转义。以为.是转义字符
//否则结果与预期的不一样
String str20 = "foo.foo1.foo2.foo3.foo4";
System.out.println(Arrays.toString(str20.split("\\.",2)));
//|作为分隔符时,也要使用\\进行转义,否则会得不到想要的结果,
//结果为:[, f, oo|foo1|foo2|foo3|foo4]
String str22 = "foo|foo1|foo2|foo3|foo4";
System.out.println(Arrays.toString(str22.split("|",3)));
//|做为分隔符的正确用法str22.split("\\|",3)
//其运行结果为:[foo, foo1, foo2|foo3|foo4]
String str23 = "foo|foo1|foo2|foo3|foo4";
System.out.println(Arrays.toString(str23.split("\\|",3)));
//当使用多个分隔符时可以这样写str.split(分隔符1|分隔符2)
//以下代码的输出结果为:[acount=? , uu =? , n=?]
String str21 = "acount=? and uu =? or n=?";
System.out.println(Arrays.toString(str21.split("and|or")));
//判断字符传里面是否包含某个值
String str24 = "我爱刘亦菲";
System.out.println(str24.contains("爱"));//true
System.out.println(str24.contains("爱刘"));//true
System.out.println(str24.contains("我刘"));//false
System.out.println("--------------------------");
String str25 = "我爱刘亦菲 ";
String str26 = "我爱刘亦菲";
boolean ok = str25.contentEquals(str26);
System.out.println(ok);
String str27 = new String("我爱刘亦菲 ");
String str28 = new String("我爱刘亦菲");
Foo foo1 = new Foo();
Foo foo2 = new Foo();
//对于下面的一个东西,有些人看了可能会有疑问。为什么会是true呢?equals不是比较地址值吗?。
//因为sun在开发String时就重写了equals方法,使它比较的是内容而不再是地址
System.out.println(str27.equals(str28));//true
System.out.println(foo1.equals(foo2));//false
System.out.println(str27.contentEquals(str28));//true
}
}
class Foo{
}