Java字符串 Java常用字符串方法小结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java字符串 Java常用字符串方法小结

并不奇怪的菜蜀黍   2021-03-24 我要评论
想了解Java常用字符串方法小结的相关内容吗,并不奇怪的菜蜀黍在本文为您仔细讲解Java字符串的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java常用字符串方法,Java字符串,下面大家一起来学习吧。

下面是对字符串操作的代码小总结。大部分是String类的 操作方法,需要的朋友可以参考下

public class StudyString {
    public static void main(String[] ergs){
        //字符串的声明与赋值
        String name = "蔡宇飞";
        String hisname = new String ("小明");
        System.out.println(name+"和"+hisname+"是好朋友");
        //字符串基本操作
        //获取字符串的长度
        //字符串名.length() 返回字符的个数
        String hello = "hello world!";
        int length = hello.length();
        System.out.println(hello+"的长度是"+length);
        //字符串连接
        //String类提供的concat()方法
        //字符串1.concat(字符串2) 返回值是一个字符串
        String twoname = name.concat(hisname);
        System.out.println(twoname);
    //字符串比较
        //String提供的equals()方法,返回值为boolean类型。两个字符串中每个字符完全一致时才为turn.否则为false
        //字符串1.equals(字符串2)
        String str1 = "fuck";
        String str2 = "FUCK";
        if (str1.equals(str2))
            System.out.println("相同");
        else
            System.out.println("不同");
        //String还提供了equalsIgnoreCase()方法,这个与上面的区别是不区分字母的大小写。返回值同样为boolean类型
        //字符串1.equalsIgnoreCase(字符串2)
        if(str1.equalsIgnoreCase(str2))
            System.out.println("相同");
        else
            System.out.println("不同");
   //字符串截取
        //从字符串中截取一部分作为新的字符串,String类提供的substring来实现
        //字符串.substring (开始位置); 或者  字符串.substring (开始位置,结束位置);
        //第一种是从开始位置直到结束,第二种从开始位置到结束位置.
        String my ="my name is caiyufei,I love Java and Python.";
        String love =my.substring(20); 
        String myname = my.substring(11, 19);
        System.out.println(love);
        System.out.println(myname);
        //字符串查找
        //在一个字符串中查找另一个字符串,String类提供了indexOf方法来实现
        //字符串1.indexOf(字符串2) 或  字符串1.indexOf(字符串2,开始位置)
        int lovenum = my.indexOf(love);
        int mynamenum = my.indexOf(myname);
        System.out.println(lovenum);
        System.out.println(mynamenum);
        //字符串替换
        //用一个新字符去替换字符串中指定的所有字符 String类提供了replace方法实现这种替换
        //字符串1.replance(被替换字符,替换字符)
        char I_ = 'I';
        char m_ = 'M';
        System.out.println(love.replace(I_, m_)); //M love Java and Python.
  //字符串与字符数组
        //将字符数组作为构造函数的参数直接转换成字符串
        char [] helloArray = {'h','e','l','l','o'};
        String helloString = new String (helloArray);
        System.out.println(helloString);
        //将字符串转为字符数组
        //toCharArray()方法
        char [] Array = helloString.toCharArray();
        for (int i = 0;i<Array.length;i++)
            System.out.println(Array[i]);
        //其他方法
        //字符串中英文字母转换为小写
        String eng = "I Love English";
        String eng_1 = eng.toLowerCase();
        System.out.println(eng_1);
        //字符串中英文字母转为大写
        String eng_2 = eng.toUpperCase();
        System.out.println(eng_2);
        //返回指定索引处的字符
        char en = eng.charAt(5);
        System.out.println(en);//e
        //比较字符串,返回int
        int num_1 = eng.compareTo(eng_1);
        System.out.println(num_1);
 //返回第一个找到的子字符串的位置,若无则返回-1
        String loves = "Love";
        int lovenum1 = eng.indexOf(loves);
        System.out.println(lovenum1); //2
        //去除字符串前后空格
        String world = "   I love my world   ";
        System.out.println(world);
        world = world.trim();
        System.out.println(world);
        //判断suffix是否为字符串的开始
        String suffix = "world";
        if (world.startsWith(suffix))
            System.out.println("world是字符串world的开始");
        else
            System.out.println("world不是字符串world的开始");
  //判断suffix是否为字符串的结尾
        if (world.endsWith(suffix))
            System.out.println("world是字符串world的结尾");
        else
            System.out.println("world不是字符串world的结尾");
        //String buffer 类
        //极大的提高字符串的处理速度,缺点是占内存大。在处理极大量字符时使用
        StringBuffer sb = new StringBuffer("StringBuffer beautifer good");
        System.out.println(sb);
        //添加参数到StringBuffer对象中
        sb.append(",");
        sb.append("I love StringBuffer!");
        System.out.println(sb);
        //删除StringBuffer对象中指定字符或字符序列
        sb.deleteCharAt(0);//删指定位置的一个字符
        System.out.println(sb);
        sb.delete(0, 12);//从某位置到某位置全删
        System.out.println(sb);
        //
    }
}

上面代码学习的朋友可以参考下

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们