String类获取功能,转换功能和一些其他功能.

一:获取功能

                 int length():获取字符串长度

                 public char charAt(int index):获取指定索引出的字符.

                 public int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引.

                 public int indexOf(String str):子字符串在大字符串中第一次出现的索引.

                 public int lastIndexOf(int ch):返回指定字符串在此次子福串中最后一次出现处的索引

                 public String  substring(int beginIndex): 从指定位置开始截取,默认截取到末尾

                 public String substring(int beginIndex,int endIdex):从指定位置开始截取,截取到指定位置结束(包前不包后)

String获取功能的举例:

package org.westos.Demo2;
/*  String中获取功能的举例:
 * 				 int length():获取字符串长度
                 public char charAt(int index):获取指定索引出的字符.
                 public int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引.
                 public int indexOf(String str):子字符串在大字符串中第一次出现的索引.
                 public int lastIndexOf(int ch):返回指定字符串在此次子福串中最后一次出现处的索引
                 public String  substring(int beginIndex): 从指定位置开始截取,默认截取到末尾
                 public String substring(int beginIndex,int endIdex):从指定位置开始截取,截取到指定位置结束(包前不包后)
 *
 * **/

public class StringDemo1 {
	public static void main(String[] args) {
		String s = "HelloWorld";
		// length():获取字符串长度
		System.out.println("length()=" + s.length());
		// indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引.
		System.out.println("indexOf(int ch)=" + s.indexOf('l'));
		// public int indexOf(String str):子字符串在大字符串中第一次出现的索引.
		System.out.println("indexOf(String str)=" + s.indexOf("llo"));
		// public int lastIndexOf(int ch):返回指定字符串在此次子福串中最后一次出现处的索引
		System.out.println("lastIndexOf(int ch)=" + s.lastIndexOf('l'));
		// public String  substring(int beginIndex): 从指定位置开始截取,默认截取到末尾
		System.out.println("substring(int beginIndex)=" + s.substring(3));
		// public String substring(int beginIndex,intendIdex):
		//从指定位置开始截取,截取到指定位置结束(包前不包后)
		System.out.println("substring(int beginIndex,int endIdex)=" + s.substring(3, 6));

	}

}

二:转换功能:

                       byte[] getBytes() :将字符串转换成字节数组
                       public char[] toCharArray():将字符串转换为字符数组
                       valueOf(XXX  变量名) ;可以将任意类型转换为String类型
                       public String toLowerCase() :将字符串转换小写
                       public String toUpperCase():将字符串转换成大写
                       public String concat(String str)将指定字符串连接到此字符串的结尾

转换功能的举例:

package org.westos.Demo2;
/*                     byte[] getBytes() :将字符串转换成字节数组
                       public char[] toCharArray():将字符串转换为字符数组
                       valueOf(XXX  变量名) ;可以将任意类型转换为String类型
                       public String toLowerCase() :将字符串转换小写
                       public String toUpperCase():将字符串转换成大写
					   public String concat(String str)将指定字符串连接到此字符串的结尾
 * */

public class StringDemo2 {
	public static void main(String[] args) {
		String s1 = "JavaSe";
		// byte[] getBytes() :将字符串转换成字节数组
		byte[] by = s1.getBytes();//// 如果是当前平台编码集:是个一个gbk,参数省略
		for (int i = 0; i < by.length; i++) {
			System.out.print(by[i] + " ");
		}

		System.out.println("-------------------------");

		// public char[] toCharArray():将字符串转换为字符数组
		char[] ch = s1.toCharArray();
		for (int j = 0; j < ch.length; j++) {
			System.out.print(ch[j] + " ");
		}

		System.out.println("-------------------------");

		// valueOf(XXX  变量名) ;可以将任意类型转换为String类型
		String s2 = s1.valueOf(ch);
		System.out.println(s2);

		System.out.println("-------------------------");

		// public String toLowerCase() :将字符串转换小写
		System.out.println(s1.toLowerCase());
		// public String toUpperCase():将字符串转换成大写
		System.out.println(s1.toUpperCase());

		System.out.println("-------------------------");

		// public String concat(String str)将指定字符串连接到此字符串的结尾
		String s3 = "Hello";
		String s4 = "World";
		System.out.println(s3.concat(s4));

	}

}

三:字符串中的其他功能

           1.替换功能

                     String replace(char oldChar, char newChar)  :将指定的字符替换以前的字符
                     String replace(String oldString, String newString):将以前的子字符串替换新的子字符串

           2.去除两端空格

                      public String trim()  

           3.按字典顺序比较

                      public int compareTo(String anotherString):字符串比较方法

其他功能举例:

package org.westos.Demo2;

/*
 * 字符串中的其他功能

       1.替换功能
            String replace(char oldChar, char newChar)  :将指定的字符替换以前的字符
            String replace(String oldString, String newString):
                                    将以前的子字符串替换新的子字符串
       2.去除两端空格
             public String trim()  
       3.按字典顺序比较
             public int compareTo(String anotherString):字符串比较方法
 * **/
public class StringDemo3 {
	public static void main(String[] args) {
		String s = "HelloWorld";
		// String replace(char oldChar, char newChar)  :将指定的字符替换以前的字符
		System.out.println(s.replace('o', 'l'));
		// String replace(String oldString, String newString):
		// 将以前的子字符串替换新的子字符串
		System.out.println(s.replace("llo", "HHH"));

		System.out.println("---------------------------------");
		// public String trim()
		String s1 = " helloworld ";
		System.out.println(s1.trim());
		System.out.println("---------------------------------");
		// public int compareTo(String anotherString):字符串比较方法
		String s2 = "Hello";
		String s3 = "HEL";
		System.out.println(s2.compareTo(s3));

	}

}

                     

猜你喜欢

转载自blog.csdn.net/qq_41942241/article/details/81260138
今日推荐