Java中String类里的一些常用方法

package usuallyClass;

import java.util.ArrayList;
import java.util.List;

public class UsualString {
    public static void main(String[] args) {
//      字符串构建的三种方法
        String s1 = "Hello,world";

        char[] c1 = {'H','e','l','l','o',',','w','o','r','l','d'};
        String s2 = new String(c1);

        String s3 = new String("Hello,world");
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println("===============");

//      根据int参数找出char值
        System.out.println(s1.charAt(0));
//      实现字符串的拼接,该方法不会改变原字符串,生成一个新字符串
        String s4 = s1.concat(s3);
        System.out.println(s4);
        System.out.println(s1);

//      endwith常用于查找文件后缀名
        String s5 = "c:/a/index.html";
//      判断字符串是否以传入的字符串参数结尾,返回值为Boolean
        System.out.println(s5.endsWith(".html"));
        System.out.println("===============");

//      对字符串内容的比较
        String s6 = "tom";
        String s7 = new String("tom");
//      原因是s6存储在常量池,s7存储在堆区,这s6和s7的内存地址是不一样的
//      而equals方法,比较的是这两个字符串的内容是否相等
        System.out.println(s6 == s7);//flase
        System.out.println(s6.equals(s7));//true

//      字符串中的indexof方法
//      1.一个String参数,返回值为int,用于返回指定字符在此字符串中第一次出现处的索引。
        System.out.println(s1.indexOf(",world"));
        String s8 = "a1a2a3a4b1b2b3b4aaa";
        System.out.println(s8.indexOf("a"));
//      2.两个参数,第一个为String,第二个参数为int型的索引值,从第二个参数处
//      来搜索第一个参数
        System.out.println(s8.indexOf("a", 3));
        System.out.println("===============");

//      通过编写方法来查询一个字符在字符串里出现了多少次
        List<Integer> indexs = getIndexsOfStr(s8, 'a');
        for (Integer index : indexs) {
            System.out.println(index + "\t");
        }
        System.out.println("===============");

        // 打印文件的后缀
        String s9 = "d:/a/index.js";
//      这里lastIndexOf方法是指在字符串的最右边开始搜索,返回传入字符串的索引位置
        int s9_1 = s9.lastIndexOf(".") + 1;
        System.out.println(s9.lastIndexOf("."));
//      该处方法是从传入参数的位置开始分割字符串,返回新的字符串
        String s9_2 = s9.substring(s9_1);
        System.out.println(s9_2);
        System.out.println("===============");

//      替换
        String s10 = "abcabcabc";
//      全部替换
        System.out.println(s10.replace("a", "A"));
//      只替换第一个匹配到的
        System.out.println(s10.replaceFirst("a", "A"));
        System.out.println("===============");

        String s12 = "a-b-c-d-e-f";
//      根据传入的参数来分配数组,简单来说,就是传入参数,字符串里有什么,就去掉什么。
        String[] s12s = s12.split("-");
//      foreach遍历
        for (String s : s12s) {
            System.out.println(s);
        }

        String s13 = "20171108001";
//      测试此字符串是否以指定的前缀开始
        System.out.println(s13.startsWith("20171108"));

//      变为大写
        System.out.println(s12.toUpperCase());

        String s14 = " hello,wolrd!  ";
//      用于去除字符串两端的空格
        String s15 = s14.trim();
        System.out.println("#"+s14+"#");
        System.out.println("#"+s15+"#");

    }
    public static List<Integer> getIndexsOfStr(String src, char c) {
//      先创建一个int包装类的集合
        List<Integer> rs = new ArrayList<Integer>();
//      判断传入的字符串是否为空
        if(null != src) {
//          将字符串通过tochararray方法改为char类型的数组
            char[] cs = src.toCharArray();
//          接下来遍历数组
            for(int i = 0; i < cs.length; i++) {
//              判断数组里的字符串是否与传入的char相同
                if(cs[i] == c) {
//                  如果相同就把i加到集合里面?这里是否是自动装箱?
                    rs.add(i);
                }
            }
        }
        return rs;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值