• 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

  • 相关阅读:
    Google Go语言推出第一个正式版本:Go 1
    前端开发工作感悟:具体的量化指标
    AIR SDK 更新方法
    HTML5 MediaStream的运用
    理解css中的长度单位
    快速提高 Vi/Vim 使用效率的原则与途径
    Saving the Day with Scoped CSS
    事件的发生顺序HTML5移动开发
    BigPipe学习研究
    构建合规的Web应用程序
  • 原文地址:https://www.cnblogs.com/cfc-blog/p/10941516.html
Copyright © 2020-2023  润新知