zoukankan      html  css  js  c++  java
  • java字符串String对象各种方法总结

    遍历String中的字符:

    int length():返回字符串长度:

    public class StringTest {
        public static void main(String[] args){
            String mystring = "HelloWorld";
            int len = mystring.length();
            System.out.println("字符串长度是:" + len);
        }
    }
    

     输出结果是10

    char charAt(int index):根据索引取字符串中某个字符,索引是从0开始的

    public class StringTest {
        public static void main(String[] args){
            String mystring = "HelloWorld";
            char s = mystring.charAt(3);
            System.out.println(s);
        }
    }

    输出结果是HelloWorld中的第四个字符l

    获取字符串中的一部分:

    String substring(int beginIndex, int endIndex):返回子字符串:

    public class StringTest {
        public static void main(String[] args){
            String mystring = "HelloWorld";
            String s = mystring.substring(0,5);
            System.out.println(s);
        }
    }

    输出为字符串hello(注意索引前包后闭),还有一个重载方法参数只有一个String substring(int beginIndex):返回从第一个索引到最后的子字符串。

    判断两个字符串是否相等:

    equals()返回boolean类型

    public class StringTest {
        public static void main(String[] args){
            String mystring = "HelloWorld";
            String hellojava = "HelloJava";
            boolean equal = mystring.equals(hellojava);
            System.out.println(equal);
        }
    }

    返回false

  • 相关阅读:
    HDU 5542 The Battle of Chibi (离散化+树状数组优化DP)
    UVaLive 7637 Balanced String (构造)
    ZOJ 3512 Financial Fraud (左偏树)
    HDU 1512 Monkey King (左偏树+并查集)
    POJ 2311 Cutting Game (博弈)
    POJ 2348 Euclid's Game (博弈)
    Gym 101142C CodeCoder vs TopForces (搜索)
    Gym
    Spring注解
    sql语句中的占位符?有什么作用
  • 原文地址:https://www.cnblogs.com/cfc-blog/p/10941516.html
Copyright © 2011-2022 走看看