zoukankan      html  css  js  c++  java
  • 字符串操作:索引位置、去空格、替换字符串

    指定字符的索引位置:s.indexOf()、s.lastIndexOf()  (以字符串s为例。第一次与最后一次出现的位置)

    指定索引位置的字符:s.charAt()

    除前、后空格:s.trim()

    去除所有空格:s.replaceAll(" ","")   替换思路

    替换第一个匹配的字符串:s.replaceFirst()

    替换所有匹配的字符串:s.replace()

        String s="students";
        //索引位置
        System.out.println("第一次出现s的位置:"+s.indexOf("s")+"
    最后一次出现s的位置:"+s.lastIndexOf("s"));
        System.out.println("在索引位置1处的字符是:"+s.charAt(1));
        String s1="   a b   ";
        //去空格
        System.out.println("去除首尾空格后:"+s1.trim());
        System.out.println("去除所有空格后:"+s1.replaceAll(" ",""));
        String s2="bad bad boy";
        //替換字符串
        System.out.println(s2.replaceFirst("bad", "good"));
        System.out.println(s2.replace("bad", "good"));

    运行结果: 

  • 相关阅读:
    函数 对象 入门总结
    js 禁止复制粘贴全选
    CSS3个人盲点总结【总结中..........】
    was设置事务超时
    阿里前DBA的故事
    转型思考
    自卑
    关于BigDecimal的使用
    少睡与吸烟影响IQ
    DB2中OLAP函数使用示例
  • 原文地址:https://www.cnblogs.com/xixixing/p/7308327.html
Copyright © 2011-2022 走看看