字符串的获取相关方法与字符串的截取方法

获取功能的方法
public int length () :返回此字符串的长度。
public String concat (String str) :将指定的字符串连接到该字符串的末尾。
public char charAt (int index) :返回指定索引处的 char值。
public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符
串结尾。
public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到
endIndex截取字符串。含beginIndex,不含endIndex。
package demox.demopro;
/*
String当中与获取相关的常用方法有;
public int length():获取字符串当中含有的字符个数,拿到字符串长度。
public String concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串。public char charAt(int index)。获取指定索引位置的单个字符。(索引从e开始。)
public int indexOf (String str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值。*/

public class String_Demo02 {
 public static void main(String[] args) {
 //创建字符串对象 
String s = "helloworld";
 // int length():获取字符串的长度,其实也就是字符个数 System.out.println(s.length()); 
System.out.println("‐‐‐‐‐‐‐‐");
 // String concat (String str):将将指定的字符串连接到该字符串的末尾. 
String s1 = "helloworld";
 String s2 = s.concat("**hello itheima");
System.out.println(s2);// helloworld**hello itheima
// char charAt(int index):获取指定索引处的字符
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println("‐‐‐‐‐‐‐‐");
// int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("ak"));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start):从start开始截取字符串到字符串结尾
System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start,int end):从start到end截取字符串。含start,不含end。
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3,8));
}
}

字符串的截取方法

package demox.demopro;
/*
public String substring(int index):截取从参数位置一直到字符串末尾,返回新字符串。
public String substring(int begin,int end):截取从begin开始,一直到end结束,中间的字符串。备注:[ begin , end),包含左边,不包含右边。
*/

import javax.crypto.spec.PSource;

public class String_Demo01 {
    public static void main(String[] args) {
        String  str1  ="HelloWorld";
        String str2 = str1.substring(5);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println("**************");

        String str3 = str1.substring(4, 7);
        System.out.println(str3);
        System.out.println("**************");

        String  strA ="Hello";
        System.out.println(strA);
        strA ="Java";
        System.out.println(strA);
    }

}

 

 

 

package demox.demopro;
/*
String当中与获取相关的常用方法有;
public int length():获取字符串当中含有的字符个数,拿到字符串长度。
public String concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串。public char charAt(int index)。获取指定索引位置的单个字符。(索引从e开始。)
public int indexOf (String str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值。*/

public class String_Demo02 {
public static void main(String[] args) {
//创建字符串对象
String s = "helloworld";
// int length():获取字符串的长度,其实也就是字符个数 System.out.println(s.length());
System.out.println("‐‐‐‐‐‐‐‐");
// String concat (String str):将将指定的字符串连接到该字符串的末尾.
String s1 = "helloworld";
String s2 = s.concat("**hello itheima");
System.out.println(s2);// helloworld**hello itheima
// char charAt(int index):获取指定索引处的字符
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println("‐‐‐‐‐‐‐‐");
// int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("ak"));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start):start开始截取字符串到字符串结尾
System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start,int end):startend截取字符串。含start,不含end
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3,8));
}
}
posted @ 2022-06-30 16:10  zj勇敢飞,xx永相随  阅读(140)  评论(0编辑  收藏  举报