JAVA学习笔记(八)- 字符串类和方法

字符串的创建

/*
 * String的创建
 */
public class Test
{
    public static void main(String[] args)
    {
        //String是引用类型,所以其创建形式可以使用new
        //引用类型,只要new创建对象,都会在堆内存中开辟一块新的内存空间,存放属性值
        String str1 = new String("abc");
        String str2 = new String("abc");
//      String str3 = new String("abc");

        //String对象创建的第二种方式
        //内存中,有一个字符串池,如果以此种形式创建String对象,此时会在String pool中
        //查找指定的字符串常量是否存在,如果不存在,则分配内存空间,创建该字符串常量;如果
        //在String pool中已存在该字符串常量,则会将该常量所在内存地址赋值给声明的对象
        String s1 = "abc";
        String s2 = "abc";
//      String s3 = "abc";

        System.out.println(str1 == str2);//false
        System.out.println(s1 == s2);//true
    }
}

/*
 * ==和equals方法
 */
public class Test
{
    public static void main(String[] args)
    {
        int a = 10;
        int b = 10;
        // 对于基本数据类型来说,其真实值存放在栈中,所以
        // a==b比较的是栈中的值是否相等。
        // 对于基本类型,==比较的是真实值。
        System.out.println(a == b);// true

        String str1 = new String("abc");
        String str2 = new String("abc");
        // 对于引用类型来说,其真实的值存放在堆中,而对象中的值是在栈中存放的对应堆内存的地址
        // str1==str2比较的不是真实的值,而是地址
        System.out.println(str1 == str2);

        String s1 = "abc";
        String s2 = "abc";
        // 对于引用类型来说,其真实的值存放在堆中,而对象中的值是在栈中存放的对应堆内存的地址
        // s1==s2比较的不是真实的值,而是地址
        System.out.println(s1 == s2);

        //如果想比较引用类型真实的值(堆上存放的值),必须使用equals方法。
        System.out.println(str1.equals(str2));
        System.out.println(s1.equals(s2));

        //如果==为true,则equals一定为true

        String ss = new String("xyz");
        String ss2 = ss;

    }
}

字符串比较

/*
 * 几种String的比较
 */
public class Test
{
    public static void main(String[] args)
    {
        String s1 = new String("abc");
        String s2 = new String("abc");
        String s3 = "ab";
        String s4 = "c";
        String s5 = "ab" + "c";
        String s6 = s3 + s4;//StringBuffer sb = new StringBuffer();sb.append(s3).append(s4)
        String s7 = "abc";

        System.out.println(s1 == s2);//false
        System.out.println(s1 == s7);//false
        System.out.println(s7 == s5);//true
        System.out.println(s7 == s6);//false
        System.out.println(s5 == s6);//false

        //3  5   7
        String s = "a" + "b" + "ab" + "d";
        //字符串通过+连接,浪费内存空间,以后涉及到字符串的连接,一定要使用StringBuffer类
    }
}

字符串对象

/*
 * String类用法
 */
public class Test
{
    public static void main(String[] args)
    {
        //创建String对象
        String s1 = new String("abc");

        char[] cs = {'a', 'b', 'c', 'd'};
        //将char[]->String
        String s2 = new String(cs);//"abcd"
        System.out.println(s2);

        //String(char[] cs, int i, int j)
        String s3 = new String(cs, 2, 2);
        System.out.println(s3);

        String s4 = "abc";
    }
}
/*
 * String方法
 */
public class Test
{
    public static void main(String[] args)
    {
        String s = "abcxyzijkabca";

        //charAt:取得指定索引出的char值
        char c = s.charAt(3);
        System.out.println(c);//x

        //length(),求字符串中元素的个数
        int length = s.length();//得到字符串的长度
        System.out.println(length);
        //数组中,有一个length属性,可以获得数组元素的长度.   arrs.length;
        //String中,有一个length方法,可以获得字符串的长度。  s.length();

        int count = 0;
        //打印输出字符串中每个字符
        for(int i = 0; i < s.length(); i++)
        {
//          System.out.println(s.charAt(i));
            char ch = s.charAt(i);
            if('a' == ch)
            {
                count++;
            }
        }
        System.out.println(count);
    }
}

字符串比较

/*
 * String方法
 */
public class Test
{
    public static void main(String[] args)
    {
        String s1 = "abc";
        String s2 = "aBc";
        //比较两个字符串是否相同。如果相同,返回0
        //如果不同,返回不同的字符之差。
        //不忽略大小写
        int result = s1.compareTo(s2);
        System.out.println(result);

        //忽略大小写比较字符串。
        int rel = s1.compareToIgnoreCase(s2);
        System.out.println(rel);

        //字符串的连接
        String str = s1.concat(s2);//abcaBc
        System.out.println(str);

        String s = "hello world hello world";
        //判断某个字符串中是否包含指定的字符串
        boolean flag = s.contains("world1");
        System.out.println(flag);
    }
}

常用方法

/*
 * String方法
 */
public class Test
{
    public static void main(String[] args)
    {
        String s1 = "hello.java";
        //用于判断字符串是否以指定的字符串结尾。
        boolean flag1 = s1.endsWith(".java");
        System.out.println(flag1);
        //判断是否以指定字符串开始
        boolean flag2 = s1.startsWith("He");
        System.out.println(flag2);
        //从指定位置开始,判断是否以指定字符串开头。
        boolean flag3 = s1.startsWith("llo", 2);
        System.out.println(flag3);

        String s2 = "abc";
        String s3 = "abc";
        System.out.println(s2 == s3);//true,表示比较的是地址
        System.out.println(s2.equals(s3));//比较的是真实值
        System.out.println(s2.equalsIgnoreCase(s3));//比较真实值,忽略大小写
    }
}

字符数组与字符串之间互相转换


/*
 * String方法
 */
public class Test
{
    public static void main(String[] args) throws UnsupportedEncodingException
    {
        String s = "我爱北京天安门";

        //字符数组与字符串之间互相转换。
        //编码,字符-按照默认字符集utf8->字节数组(二进制)
        byte[] bs = s.getBytes();
        //解码,二进制-按照默认字符集utf8-解码成字符
        String str = new String(bs);
        System.out.println(str);

        //编码,以指定字符集GBK,将字符->字节数组 GBK
        byte[] bs2 = s.getBytes("GBK");
        //解码
        String str2 = new String(bs2, "GBK");
        System.out.println(str2);

        //字符数组与字符串之间互相转换
        //字符串->字符数组
        char[] cs = s.toCharArray();
        for(char c:cs)
        {
            System.out.println(c);
        }
        //字符数组->字符串
        String ss = new String(cs);
    }
}

字符串索引


/*
 * String方法
 */
public class Test
{
    public static void main(String[] args)
    {
        String s = "hello world hello world hello world";
        //indeOf
        //表示字符o第一次在s字符串中出现的索引位置
        int index1 = s.indexOf('o');
        System.out.println(index1);
        //从指定位置开始向后查找,第一次出现字符w的索引
        int index2 = s.indexOf('w', 10);
        System.out.println(index2);
        //查找指定字符串在s中第一次出现的位置
        int index3 = s.indexOf("world");
        System.out.println(index3);
        int index4 = s.indexOf("world", 10);
        System.out.println(index4);
        //lastIndexOf
        //o在s中,最后一次出现的索引
        int index5 = s.lastIndexOf('o');
        System.out.println(index5);
        int index6 = s.lastIndexOf('o', 10);
        System.out.println(index6);
        int index7 = s.lastIndexOf("world");
        System.out.println(index7);
        int index8 = s.lastIndexOf("world", 31);
        System.out.println(index8);

        String ss = "";
        System.out.println(ss.isEmpty());
    }
}

字符串截取

/*
 * String方法
 */
public class Test
{
    public static void main(String[] args)
    {
        String s = "hello world";

        //表示截取s字符串,从索引为6的地方开始,知道s结束
        String ss1 = s.substring(6);
        System.out.println(ss1);
        //截取s的一部分,[6, 9)
        String ss2 = s.substring(6, 9);//begin<=    <end
        System.out.println(ss2);

        String s2 = "abcABCxxx";
        System.out.println(s2.toUpperCase());
        System.out.println(s2.toLowerCase());

        //忽略字符串前后的空格
        String s3 = "   a  aa   ";
        String ss3 = s3.trim();
        System.out.println(ss3);

        String s4 = "\tabc\t";
        System.out.println(s4);
        System.out.println(s4.trim());

    }
}

字符串替换

/*
 * String方法
 */
public class Test
{
    public static void main(String[] args)
    {
        String s1 = "hello world hello world";
        //将s1中l替换成大写的L
        String ss1 = s1.replace('l', 'L');
        System.out.println(ss1);
        //将hello替换成HELLO
        String ss2 = s1.replace("hello", "HELLO");
        System.out.println(ss2);

        String s2 = "i am a good man";
        //表示以" "拆分字符串
        String[] strs = s2.split(" ");
        for(String s : strs)
        {
            System.out.println(s);
        }

        String s3 = "aa\tbb\tcc\tdd";
        String[] str3 = s3.split("\t");
        System.out.println(str3.length);
        System.out.println(s3);

        String s4 = "aa  bb cc  dd";
        String[] str4 = s4.split(" ");
        System.out.println(str4.length);
        for(String s : str4)
        {
            System.out.println(s);
        }
    }
}

基本数据类型->String类型

/*
 * 基本数据类型-》String类型
 */
public class Test
{
    public static void main(String[] args)
    {
        //基本数据类型-》String类型
        //10 + "" -> String类型
        String s1 = String.valueOf(true);//"true"
        System.out.println(s1);
        String s2 = String.valueOf('a');//"a"
        System.out.println(s2);
        String s3 = String.valueOf(10.0F);//"10.0"
        System.out.println(s3);
        String s4 = String.valueOf(20.05);
        System.out.println(s4);//"20.05"
        String s5 = String.valueOf(10000);//"10000"
        System.out.println(s5);
        String s6 = String.valueOf(2000L);//"2000"
        System.out.println(s6);

        //字符数组->String
        //new String(cs);
        char[] cs = {'a', 'b', 'c'};
        String s7 = String.valueOf(cs);//"abc"
        System.out.println(s7);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值