zoukankan      html  css  js  c++  java
  • Java_String的操作

    1.public char charAt(int index)
    返回字符串中第index个字符;

    String  s1 = "Hello World"

    System.out.println(s1.charAt(4)) // 输出o

    2.public int length()
    返回字符串的长度;

    System.out.println(s1.length) // 输出11

    3.public String[] split(String regex)
    将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组

     String date = "2008/09/10";
                String[ ] dateAfterSplit= new String[3];
                dateAfterSplit=date.split("/");         //以“/作为分隔符来分割date字符串,并把结果放入3个字符串中。

                for(int i=0;i<dateAfterSplit.length;i++)
                           System.out.print(dateAfterSplit[i]+" ");
          }

    输出结果:2008 09 10

    4.public String substring(int beginIndex,int endIndex)
    返回该字符串从beginIndex开始到endsIndex结尾的子字符串

    System.out.println(s1.substring(1,3)) // 输出ell字符串

    5.public String replace(char oldchar,char newChar)
    在字符串中用newChar字符替换oldChar字符

    System.out.println(s1.replace("o" ,"123")) // 输出Hell123 World字符串

    6.int compareTo(String anotherString) 

    当前String对象与anotherString比较

    String s2 = "HelloWorld"

    String s3 = "Hello World"

    String s4 = "HelloWorldabc"

    System.out.println(s1.compareTo(s2)) // 输出5

    System.out.println(s1.compareTo(s3)) // 输出0

     

  • 相关阅读:
    Django restfull规范
    Python3中的zip()
    关于负数的isdigit()判断
    Win7 x64安装Paramiko
    深度学习性能提高
    神经网络激励函数
    机器学习十大算法
    深度学习十大框架比较
    python 换行
    python sort() sorted() 与argsort()函数的区别
  • 原文地址:https://www.cnblogs.com/lcl15/p/6899910.html
Copyright © 2011-2022 走看看