String方法阅读笔记

String类常用方法

 

1.int Length():

  参数:无

  返回值:调用此方法的字符串的长度(int)

  实例:

 1 public class Test {
 2     public static void main(String args[]) {
 3         String Str1 = new String("www.cnblogs.com");
 4         String Str2 = new String("cnblogs" );
 5 
 6         System.out.print("字符串 Str1 长度 :");
 7         System.out.println(Str1.length());
 8         System.out.print("字符串 Str2 长度 :");
 9         System.out.println(Str2.length());
10     }
11 }

2.char charAt(int index):

  charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。

  参数:index:字符串索引

  返回值:返回指定索引处的字符

  实例:

1 public class Test {
2 
3     public static void main(String args[]) {
4         String s = "www.cnblogs.com";
5         char result = s.charAt(8);
6         System.out.println(result);
7     }
8 }

3.void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin):

  将字符从此字符串复制到目标字符数组。

  参数:

  • srcBegin -- 字符串中要复制的第一个字符的索引。

  • srcEnd -- 字符串中要复制的最后一个字符之后的索引。

  • dst -- 目标数组。

  • dstBegin -- 目标数组中的起始偏移量。

  返回值:无

    实例:

 1 public class Test {
 2     public static void main(String args[]) {
 3         String Str1 = new String("www.cnblogs.com");
 4         char[] Str2 = new char[6];
 5 
 6         try {
 7             Str1.getChars(4, 10, Str2, 0);
 8             System.out.print("拷贝的字符串为:" );
 9             System.out.println(Str2 );
10         } catch( Exception ex) {
11             System.out.println("触发异常...");
12         }
13     }
14 }

4.String replace(char oldChar, char newChar)

       replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。

  参数:

  • oldChar -- 原字符。
  • newChar -- 新字符。

  返回值:替换后生成的新字符串。

  实例:

 1 public class Test {
 2     public static void main(String args[]) {
 3         String Str = new String("hello");
 4 
 5         System.out.print("返回值 :" );
 6         System.out.println(Str.replace('o', 'T'));
 7 
 8         System.out.print("返回值 :" );
 9         System.out.println(Str.replace('l', 'D'));
10     }
11 }

5. String toUpperCase()

         toUpperCase() 方法将字符串小写字符转换为大写。

         参数:无

         返回值:字符转换为大写后的字符串。

    实例:

1 public class Test {
2     public static void main(String args[]) {
3         String Str = new String("www.cnblogs.com");
4 
5         System.out.print("返回值 :" );
6         System.out.println( Str.toUpperCase() );
7     }
8 }

6. String toLowerCase()

         toLowerCase() 方法将字符串转换为小写。

         参数:无

         返回值:字符转换为小写后的字符串。

    实例:

1 public class Test {
2     public static void main(String args[]) {
3         String Str = new String("WWW.CNBLOGS.COM");
4 
5         System.out.print("返回值 :" );
6         System.out.println( Str.toLowerCase() );
7     }
8 }

7. String trim()

         trim() 方法用于删除字符串的头尾空白符。

         参数:无

         返回值:删除头尾空白符的字符串。

    实例:

 1 public class Test {
 2     public static void main(String args[]) {
 3         String Str = new String("    www.cnblogs.com    ");
 4         System.out.print("原始值 :" );
 5         System.out.println( Str );
 6 
 7         System.out.print("删除头尾空白 :" );
 8         System.out.println( Str.trim() );
 9     }
10 }

8. char[] toCharArray()

         toCharArray() 方法将字符串转换为字符数组。

         参数:无

         返回值:字符数组。

    实例:

1 public class Test {
2     public static void main(String args[]) {
3         String Str = new String("www.cnblogs.com");
4 
5         System.out.print("返回值 :" );
6         System.out.println( Str.toCharArray() );
7     }
8 }
原文地址:https://www.cnblogs.com/lzq666/p/7725412.html