posts - 397,comments - 0,views - 25332

String当中与获取相关的常用方法有:

public int length():获取字符串当中含有的字符个数,拿到字符串长度。

public String concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串。

public char charAt(int index):获取指定索引位置的单个字符。(索引从0开始。)

public int indexOf(String str)t查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值。

代码举例:

复制代码
public static void main(String[] args) {

      int length = "asdsdfgfdfghjk".length();
        System.out.println(length);
      String s1 = "Hello";
      String s2 = "Hello";
        String concat = s1.concat(s2);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(concat);
        char c = "Hello".charAt(1);
        System.out.println(c);
        System.out.println("======");

        String or  = "HelloWord";
        int i = or.indexOf("llo");
        System.out.println(i);
    }
复制代码

 

字符串的截取方法:

public String substring(int index):截取从参数位置一直到字符串末尾,返回新字符串。

public String substring(int begin,int end):截取从begin开始,一直到end结束,中间的字符串。

备注: [ begin ,end),包含左边,不包含右边。

 

代码举例:

复制代码
public static void main(String[] args) {

     String s1 = "HelloWord";
     String s2 =s1.substring(5);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println("========");

        String substring = s1.substring(4, 7);
        System.out.println(substring);
        System.out.println("=========");

        //下面这种写法,字符串的内容仍然是没有改变的
        //下面有两个字符串:"HeLLo"","Java"
        //strA当中保存的是地址值。
        //本来地址值是HeLLo的ex666,
        //后来地址值变成了Java的ex999
        String stA = "Hello";
        System.out.println(stA);
        stA = "Java";
        System.out.println(stA);
    }
复制代码

 

posted on   淤泥不染  阅读(138)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示