Java中的字符串类|集1

字符串是一个字符序列。在java中,字符串的对象是不可变的,这意味着一个常量,一旦创建就不能更改。

null

创建字符串

有两种方法可以在Java中创建字符串:

  • 字符串文字
    String s = “GeeksforGeeks”;
  • 使用 关键词
    String s = new String (“GeeksforGeeks”);

建设者

  1. 字符串(字节[]字节\u arr) –通过解码 字节数组 .它使用平台的默认字符集进行解码。 例子:
    byte[] b_arr = {71, 101, 101, 107, 115};
    String s_byte =new String(b_arr); //Geeks
    
  2. 字符串(字节[]字节\u arr,字符集字符集) –通过解码 字节数组 .它使用 字符集 用于解码。 例子:
    byte[] b_arr = {71, 101, 101, 107, 115};
    Charset cs = Charset.defaultCharset();
    String s_byte_char = new String(b_arr, cs); //Geeks
    
  3. 字符串(字节[]字节\数组,字符串字符\集合\名称) –通过解码 字节数组 .它使用 字符集名称 用于解码。 它看起来类似于上面的构造,它们出现在类似函数之前,但需要 字符串(包含字符集名称) 作为参数,而上述构造函数 查塞特。 例子:
    byte[] b_arr = {71, 101, 101, 107, 115};
    String s = new String(b_arr, "US-ASCII"); //Geeks
    
  4. 字符串(字节[]字节\u arr,整数开始\u索引,整数长度) –从 字节数组 取决于 开始索引(开始位置) 长度(从起始位置开始的字符数)。 例子:
    byte[] b_arr = {71, 101, 101, 107, 115};
    String s = new String(b_arr, 1, 3); // eek
    
  5. 字符串(字节[]字节\数组,整数开始\索引,整数长度,字符集字符集) –从 字节数组 取决于 开始索引(开始位置) 长度(起始位置的字符数) .使用 字符集 用于解码。 例子:
    byte[] b_arr = {71, 101, 101, 107, 115};
    Charset cs = Charset.defaultCharset();
    String s = new String(b_arr, 1, 3, cs); // eek
    
  6. 字符串(字节[]字节\u arr,整数开始\u索引,整数长度,字符串字符集\u名称) –从 字节数组 取决于 开始索引(开始位置) 长度(起始位置的字符数) .使用 字符集名称 用于解码。 例子:
    byte[] b_arr = {71, 101, 101, 107, 115};
    String s = new String(b_arr, 1, 4, "US-ASCII"); // eeks
    
  7. 字符串(char[]char_arr) –从给定字符串中分配新字符串 字符数组 例子:
    char char_arr[] = {'G', 'e', 'e', 'k', 's'};
    String s = new String(char_arr); //Geeks
    
  8. 字符串(char[]char_数组,int start_索引,int count) –从给定的 字符数组 但是选择 计数 电影中的人物 开始索引 . 例子:
    char char_arr[] = {'G', 'e', 'e', 'k', 's'};
    String s = new String(char_arr , 1, 3); //eek
    
  9. 字符串(int[]单位代码点,int偏移量,int计数) –从 uni_代码_数组 但是选择 计数 电影中的人物 开始索引 . 例子:
    int[] uni_code = {71, 101, 101, 107, 115};
    String s = new String(uni_code, 1, 3); //eek
    
  10. 字符串(字符串缓冲区s_缓冲区) –从中的字符串中分配新字符串 s_缓冲区 例子:
    StringBuffer s_buffer = new StringBuffer("Geeks");
    String s = new String(s_buffer); //Geeks
    
  11. 字符串(StringBuilder s_builder) –从中的字符串中分配新字符串 s_建筑商 例子:
    StringBuilder s_builder = new StringBuilder("Geeks");
    String s = new String(s_builder); //Geeks
    

字符串方法

  1. int length(): 返回字符串中的字符数。
    "GeeksforGeeks".length();  // returns 13
  2. 字符字符(int i) : 返回i处的字符 th 指数
    "GeeksforGeeks".charAt(3); // returns  ‘k’
  3. 字符串子字符串(int i) : 从i返回子字符串 th 索引字符以结束。
    "GeeksforGeeks".substring(3); // returns “ksforGeeks”
  4. 字符串子字符串(int i,int j) : 返回从i到j-1索引的子字符串。
     "GeeksforGeeks".substring(2, 5); // returns “eks”
  5. 字符串concat(字符串str) : 将指定的字符串连接到此字符串的结尾。
     String s1 = ”Geeks”;
     String s2 = ”forGeeks”;
     String output = s1.concat(s2); // returns “GeeksforGeeks”
    
  6. int indexOf(字符串s) : 返回指定字符串第一次出现的字符串内的索引。
     String s = ”Learn Share Learn”;
     int output = s.indexOf(“Share”); // returns 6
    
  7. int indexOf(字符串s,int i) : 返回指定字符串第一次出现的字符串内的索引,从指定的索引开始。
     String s = ”Learn Share Learn”;
     int output = s.indexOf("ea",3);// returns 13
    
  8. Int lastIndexOf(字符串s) : 返回指定字符串最后一次出现的字符串内的索引。
     String s = ”Learn Share Learn”;
     int output = s.lastIndexOf("a"); // returns 14
    
  9. 布尔等于(对象otherObj): 将此字符串与指定的对象进行比较。
     Boolean out = “Geeks”.equals(“Geeks”); // returns true
     Boolean out = “Geeks”.equals(“geeks”); // returns false
    
  10. boolean equalsIgnoreCase(字符串另一个字符串) : 将字符串与另一个字符串进行比较,忽略大小写考虑。
     Boolean out= “Geeks”.equalsIgnoreCase(“Geeks”); // returns true
     Boolean out = “Geeks”.equalsIgnoreCase(“geeks”); // returns true
  11. int compareTo(字符串另一个字符串) : 按字典顺序比较两个字符串。
     int out = s1.compareTo(s2);  // where s1 ans s2 are
                                 // strings to be compared
    
     This returns difference s1-s2. If :
     out < 0  // s1 comes before s2
     out = 0  // s1 and s2 are equal.
     out > 0   // s1 comes after s2.
    
  12. int compareToIgnoreCase(字符串另一个字符串): 按字典顺序比较两个字符串,忽略大小写。
     int out = s1.compareToIgnoreCase(s2);  
    // where s1 ans s2 are 
    // strings to be compared
    
     This returns difference s1-s2. If :
     out < 0  // s1 comes before s2
     out = 0   // s1 and s2 are equal.
     out > 0   // s1 comes after s2.
    

    注意-在这种情况下,它将不考虑字母的情况(它将忽略它是大写还是小写)。

  13. 字符串toLowerCase() : 将字符串中的所有字符转换为小写。
    String word1 = “HeLLo”;
    String word3 = word1.toLowerCase(); // returns “hello"
    
  14. 字符串toUpperCase() : 将字符串中的所有字符转换为大写。
    String word1 = “HeLLo”;
    String word2 = word1.toUpperCase(); // returns “HELLO”
    
  15. 字符串修剪() : 通过删除字符串两端的空白,返回字符串的副本。它不影响中间的白色空间。
    String word1 = “ Learn Share Learn “;
    String word2 = word1.trim(); // returns “Learn Share Learn”
    
  16. 字符串替换(char oldChar,char newChar) : 通过替换所有出现的 奥德查尔 具有 纽查尔。
    String s1 = “feeksforfeeks“;
    String s2 = “feeksforfeeks”.replace(‘f’ ,’g’); // returns “geeksgorgeeks”
    

    注:-s1仍然是FeekForFeeks,s2是GeekGorGeeks

    用于演示所有字符串方法的程序:

    // Java code to illustrate different constructors and methods
    // String class.
    import java.io.*;
    import java.util.*;
    class Test
    {
    public static void main (String[] args)
    {
    String s= "GeeksforGeeks" ;
    // or String s= new String ("GeeksforGeeks");
    // Returns the number of characters in the String.
    System.out.println( "String length = " + s.length());
    // Returns the character at ith index.
    System.out.println( "Character at 3rd position = "
    + s.charAt( 3 ));
    // Return the substring from the ith  index character
    // to end of string
    System.out.println( "Substring " + s.substring( 3 ));
    // Returns the substring from i to j-1 index.
    System.out.println( "Substring  = " + s.substring( 2 , 5 ));
    // Concatenates string2 to the end of string1.
    String s1 = "Geeks" ;
    String s2 = "forGeeks" ;
    System.out.println( "Concatenated string  = " +
    s1.concat(s2));
    // Returns the index within the string
    // of the first occurrence of the specified string.
    String s4 = "Learn Share Learn" ;
    System.out.println( "Index of Share " +
    s4.indexOf( "Share" ));
    // Returns the index within the string of the
    // first occurrence of the specified string,
    // starting at the specified index.
    System.out.println( "Index of a  = " +
    s4.indexOf( 'a' , 3 ));
    // Checking equality of Strings
    Boolean out = "Geeks" .equals( "geeks" );
    System.out.println( "Checking Equality  " + out);
    out = "Geeks" .equals( "Geeks" );
    System.out.println( "Checking Equality  " + out);
    out = "Geeks" .equalsIgnoreCase( "gEeks " );
    System.out.println( "Checking Equality " + out);
    //If ASCII difference is zero then the two strings are similar
    int out1 = s1.compareTo(s2);
    System.out.println( "the difference between ASCII value is=" +out1);
    // Converting cases
    String word1 = "GeeKyMe" ;
    System.out.println( "Changing to lower Case " +
    word1.toLowerCase());
    // Converting cases
    String word2 = "GeekyME" ;
    System.out.println( "Changing to UPPER Case " +
    word2.toUpperCase());
    // Trimming the word
    String word4 = " Learn Share Learn " ;
    System.out.println( "Trim the word " + word4.trim());
    // Replacing characters
    String str1 = "feeksforfeeks" ;
    System.out.println( "Original String " + str1);
    String str2 = "feeksforfeeks" .replace( 'f' , 'g' ) ;
    System.out.println( "Replaced f with g -> " + str2);
    }
    }

    
    

    输出:

    String length = 13
    Character at 3rd position = k
    Substring ksforGeeks
    Substring = eks
    Concatenated string = GeeksforGeeks
    Index of Share 6
    Index of a = 8
    Checking Equality false
    Checking Equality true
    Checking Equality false
    the difference between ASCII value is=-31
    Changing to lower Case geekyme
    Changing to UPPER Case GEEKYME
    Trim the word Learn Share Learn
    Original String feeksforfeeks
    Replaced f with g -> geeksgorgeeks
    

    对于Set–2,您可以参考: JAVAJava | Set 2中的lang.String类

    本文由 拉胡尔·阿格拉瓦尔。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享