• String字符串相关操作


    .length 字符串长度
    .equals 比较字符串
    .equalIgnoreCase 比较字符串不区别大小写
    .charAt 获取字符串指定下标位置的字符
    .contains 判断字符串内是否包含某字符串
    .endsWith 判断字符串是不是以某字符串结尾
    .startsWith 判断字符串是不是以某字符串开头
    .trim 去除字符串前后的空格,不包括字符串内的
    .substring 获取字符串的从制定下标开始的字符串

    public
    class TestStr06 { public static void main(String[] args) { String s="helloworld"; System.out.println("============1长度==================="); System.out.println("长度:"+s.length()); System.out.println("============2字符串内容的比较==================="); System.out.println(s.equals("Hello")); System.out.println(s.equalsIgnoreCase("HelloWOrld")); System.out.println("============3指定位置的字符==================="); System.out.println("字符d:"+s.charAt(s.length()-1)); System.out.println("============4判断是否包含==================="); System.out.println(s.contains("hehe")); System.out.println("============5 以XXX结尾==================="); System.out.println("是否以"ld"结尾:"+s.endsWith("ld")); System.out.println("============6 以XXX开头==================="); System.out.println("是否以"he"开头:"+s.startsWith("he")); System.out.println("============7 过滤2边的空格==================="); // 3个位置都是2个空格 s=" hello world "; System.out.println("过滤前的:"+s); System.out.println("过滤前的长度:"+s.length()); System.out.println("过滤后的:"+s.trim()); System.out.println("过滤后的长度:"+s.trim().length()); System.out.println("============8 截取子串==================="); s="helloworld"; //[2,4) 左闭右开 ,[2,3] System.out.println("截取"ll":"+s.substring(2, 4)); //[2,6) [from, to) 截取的字符个数= to-from ,from是起始下标 System.out.println("截取"llow":"+s.substring(2, 6)); // from : 从from到最后 System.out.println("截取"w到最后":"+s.substring(5)); System.out.println("截取"w到最后":"+s.substring(5,s.length())); }
    }

    
    
    
    
    StringBuffer:线程安全的,效率不高,适用于多线程
    StringBuilder:线程不安全的,效率高,适用于单线程   ,如果对线程安全没有要求,优先选用StringBuilder
    /**
     * StringBuffer/StringBuilder: 用法基本相同    ,可变字符序列
     * 构造 
     * @author Administrator
     *
     */
    public class TestStr05 {
        public static void main(String[] args) {
            System.out.println("============1==============");
            //无参构造  默认长度可以存16个字符
            StringBuffer sbu = new StringBuffer();
            //append()可以写多个,没有限制
            sbu.append("helloworldhelloworld")
            .append("a")
            .append("b")
            .append("c");
            System.out.println("sbu="+sbu);
            
            
            System.out.println("============2==============");
            // 10:指的初始容量 ,初始的字符个数
            sbu = new StringBuffer(10);
            sbu.append("helloworldhelloworld")
            .append("a")
            .append("b")
            .append("c");
            System.out.println("sbu="+sbu);
            
            
            System.out.println("============3==============");
    //        StringBuffer(String str) 
            String s1="today";
            sbu = new StringBuffer(s1);
            System.out.println("sbu="+sbu);
        }
    }
    
    

     

     

    
    
    
    

     

    .indexOf 获取字符串中指定元素的下标索引

    .lastIndexOf 获取字符串中指定元素最后一次出现的下标索引

    .replaceAll 替换全部的某元素

    .replaceFirst 替换首次出现的某字符

    int num = Integer.parseInt(s) 将字符串装换成int型

    String result=String.valueOf(num3); 将其他类型转换为字符串

    /**
     *   java.lang.NumberFormatException: For input string: "112"  数据格式异常
     * @author Administrator
     *
     */
    public class TestStr07 {
        public static void main(String[] args) {
            String s="helloworld";
            System.out.println("============1索引位置===================");
            //找不到 返回-1
            System.out.println("l的下标位置:"+s.indexOf("l"));  //2
            System.out.println("l的最后一次出现下标位置:"+s.lastIndexOf("l"));   //8
            System.out.println("KK的下标位置:"+s.indexOf("KK"));
            
            System.out.println("============2 替换===================");
            System.out.println("替换掉所有的l:"+s.replaceAll("l", "*")); //he**owor*d
            System.out.println("替换首个l:"+s.replaceFirst("l", "*")); //he*loworld
            
            
            System.out.println("替换掉所有的小写字母:"+s.replaceAll("[a-z]", "*")); //**********
            System.out.println("替换掉所有的3个小写字母:"+s.replaceAll("[a-z]{3}", "*")); //***d
            System.out.println("替换掉所有的3个小写字母:"+s.replaceAll("[a-z]{4}", "*")); //**ld
            
            
            System.out.println("============3字符串转整数===================");
            s="123";
            int num=Integer.parseInt(s);
            System.out.println("字符串转换成int整数:"+num);
            
            s="1110";
    //        s="112";
            //2进制的“1110” 转成10进制
            num=Integer.parseInt(s,2);
            System.out.println("字符串转换成int整数:"+num);
            
            s="173";
            num=Integer.parseInt(s,8);
            //8进制    64+56+3=123
            System.out.println("字符串转换成int整数:"+num);
            
            s="1e";
            num=Integer.parseInt(s,16);
            //16进制    16+14=30
            System.out.println("字符串转换成int整数:"+num);
            
            System.out.println("============3字符串转浮点数===================");
            s="123.33";
            double num2 = Double.parseDouble(s);
            System.out.println("字符串转换成double浮点数:"+num2);
            float num3 = Float.parseFloat(s);
            System.out.println("字符串转换成float浮点数:"+num3);
            
            System.out.println("============4其他类型转换成String字符串===================");
            String result=String.valueOf(num3);
            System.out.println("float---》String:"+result);
        }
    }

     

     

     

  • 相关阅读:
    SMTP协议原始命令码和工作原理[转]
    VC++ SMTP协议电子邮件传送剖析
    C++编译模式(转)
    IE6下select标签覆盖div的完美解决办法
    星级评论插件
    CSS Reset CSS Framework
    if判断IE浏览器的类型
    通过Prototype属性添加Array删除重复数据方法
    Javascript性能优化
    我自己的Javascript 库,封装了一些常用函数
  • 原文地址:https://www.cnblogs.com/MRCH/p/11715183.html
Copyright © 2020-2023  润新知