zoukankan      html  css  js  c++  java
  • 总结String类的常用方法

    总结String类的常用方法

    1. 获取字符串长度

     public int length()
    

    2. 获取字符串某一位置的字符

    public char charAt(int index)
    

    注意:字符串中第一个字符索引是0,最后一个是length()-1。

    3.获取字符串的子串

    public String substring(int beginIndex)
    //该方法从beginIndex位置起,
    //从当前字符串中取出剩余的字符作为一个新的字符串返回。
    
    public String substring(int beginIndex, intendIndex)
    //该方法从beginIndex位置起,从当前字符串中
    //取出到endIndex-1位置的字符作为一个新的字符串返回。
    

    4.字符串的比较

    public int compareTo(String str)
    //该方法是对字符串内容按字典顺序进行大小比较,
    //通过返回的整数值指明当前字符串与参数字符串的大小关系。
    //若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
    
    public int compareToIgnoreCase (String str)
    //与compareTo方法相似,但忽略大小写。
    
    public boolean equals(Object obj)
    //比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
    
    public boolean equalsIgnoreCase(String str)
    //与equals方法相似,但忽略大小写。
    

    5.查找子串在字符串中的位置

    public int indexOf(String str)
    //用于查找当前字符串中字符或子串,返回字符或
    //子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
    
    public int indexOf(String str, intfromIndex)
    //改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
    
    public int lastIndexOf(String str)
    //该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
    
    public int lastIndexOf(String str, intfromIndex)
    //该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
    

    6.字符串中字符的大小写转换

    public String toLowerCase()
    //返回将当前字符串中所有字符转换成小写后的新串
    
    public String toUpperCase()
    //返回将当前字符串中所有字符转换成大写后的新串
    

    7.字符串两端去空格

    String trim()
    //去除字符串两端的空格,中间的空格不变,一般用于登陆注册时
    

    8.将字符串分割成字符串数组

    String[] split(String str)
    

    9.基本类型转换为字符串

    static String valueOf(xxx xx)
    
        
    //基本类型转化为字符串的三种方法
    int c=10;
    //包装类的toString方法
    String str1=Integer.toString(c);
    System.out.println("toString 方法转化的字符串:"+str1);
    //使用String类的valueOf()方法
    String str2=String.valueOf(c);
    System.out.println("使用String类的valueOf()方法转化的字符串"+str2);
    //一个空字符串加上基本类型,得到基本类型数据对应的字符串
    String str3=c+"";
    System.out.println("使用控制符串添加的到的字符串:"+str3);
    

    10.替换字符串

    public String replace(char oldChar, charnewChar)
    //用字符newChar替换当前字符串中所有的oldChar字符,
    //并返回一个新的字符串。
    
    public String replaceFirst(String regex,String replacement)
    //该方法用字符replacement的内容替换当前字符串中遇到的
    //第一个和字符串regex相匹配的子串,应将新的字符串返回。
    
    public String replaceAll(String regex,String replacement)
    //该方法用字符replacement的内容替换当前字符串中遇到的所有
    //和字符串regex相匹配的子串,应将新的字符串返回。
    
  • 相关阅读:
    开始学习编写用于 Windows SideShow 设备的小工具【转】
    Windows Mobile 6.5 Developer Tool Kit 下载
    Microsoft Security Essentials 微软免费杀毒软件下载
    SQL Server 2008 空间数据存储摘抄(SRID 点 MultiPoint LineString MultiLineString 多边形 MultiPolygon GeometryCollection)
    Vista Sidebar Gadget (侧边栏小工具)开发教程 (2)
    Vista Sidebar Gadget (侧边栏小工具)开发教程 (4)
    负载测试、压力测试和性能测试的异同
    Windows Server 2008 Vista Sidebar Gadget (侧边栏小工具) 入门开发实例
    Silverlight Tools 安装失败 解决办法
    SQL Server 2008 空间数据库 空间索引概念及创建(取自帮助)
  • 原文地址:https://www.cnblogs.com/codingcc1/p/11229928.html
Copyright © 2011-2022 走看看