java String class entry

  java String class entry

  1 Overview

  As long as the string is called double quotes

  Java.lang.String String class in the package, as long as the java.lang package, when writing a program does not need to import to come

  Contents of the string once created, no longer change, is a constant

  It is precisely because the string can not be changed, so the strings can be shared use. Imagine if you can share things easily modified with a residue once, who wanted to share it, think of all sick, everyone can use a shared bike, you can easily use when representatives spoil it, dismantle it ???

  The effect is equivalent to a string char [] array of characters, but the underlying principle is the byte [] array

  2. Create a String class (3 + 1 ways)

  2.1 Method three configurations

  public String (); create a blank string does not contain any content

  public String (char [] array); the contents of the string array to create the corresponding character string

  public String (byte [] array); according to the content byte array to create the corresponding character string

  1

  2

  3

  Three configurations way to demonstrate it again in code

  public class ConstructString {

  public static void main(String[] args) {

  // The first constructor: public String (); create a blank string does not contain any content

  String str1 = new String();

  System.out.println ( "first string:" + str1); // first string:

  // The second constructor: public String (char [] array); the contents of the string array to create the corresponding character string

  char[] charArray = {'A', 'B', 'C'};

  String str2 = new String(charArray);

  System.out.println ( "second string:" + str2); // second string: ABC

  The third // constructor: public String (byte [] array); according to the content byte array to create the corresponding character string

  byte [] byteArray = {97, 98, 99}; // Note that the byte array element, corresponding to the value of the ASCII character code table

  String str3 = new String(byteArray);

  System.out.println ( "third string:" + str3); // third string: abc

  }

  }

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  2.2 A direct creation

  String str4 = "HelloWorld!"

  1

  Whether or not new, as long as the stuff inside double quotes, JVM will be judged to be a string, so you can directly create

  3. constant pool

  3.1 string of relatively little knowledge about

  We want to know, in java, using if (String1 == String2) to compare two strings, the comparison is whether the addresses of the two strings of the same, and if they want to know whether the value of the same, using a String equals methods, such as:

  public static void main(String[] args) {

  String str1 = new String("hello");

  String str2 = new String("hello");

  // compare str1 address and the address is not the same as str2

  System.out.println (str1 == str2); // false is not the same

  // compare the value of str1 and str2 is not the same

  System.out.println (str1.equals (str2)); // true, it is hello

  }

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  3.2 heap constant pool (heap) in

  Str1 and str2 result of the comparison is true, prove that they are the same string, but there are only two names, in memory of all point to the same address, they are in a place called constant pool, so the constant pool of characters hello string can be shared, not be modified.

  str1 and str3 not the same as proof of their addresses are not the same, string created by the new, not in the constant pool, but also a constant

  public class ConstantPool {

  public static void main(String[] args) {

  String str1 = "hello";

  String str2 = "hello";

  String str3 = new String("hello");

  System.out.println(str1 == str2); //true

  System.out.println(str1 == str3); //false

  System.out.println(str2 == str3); //false

  }

  }

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  A method of comparing strings

  4.1 String equals method

  3. Tips from the constant pool, we know the value of comparing two strings if the same, using the equals method, the return value is a Boolean value, the same is true, is not the same as false.

  public boolean equals(Object obj)

  1

  NOTE: Any object can be received by Object, String class of course be.

  equals method has symmetry, i.e. the result str1.equals (str2) and str2.equals (str1) is the same

  Note: If you compare the two sides is a constant a variable, string constants recommendation EDITORIAL

  That recommendation "abc" .equals (str) is not recommended: str.equals ( "abc")

  Because if the string str is a null, the result of "abc" .equals (str) is certainly false, but str.equals ( "abc") written will result in a null pointer exception NullPointerException, do not casually do an exception out .

  4.1 String method of equalsIgnoreCase

  The difference between it and the equals method is that it ignores the case, Ignore to ignore, Case case

  public static void main(String[] args) {

  String str1 = "hello";

  String str2 = "HellO";

  // value comparison of the value of str1 and str2 is not as rigorous case-sensitive

  System.out.println( str1.equals(str2) ); //false

  // compare the value of str1 and str2 is not the same, ignoring case

  System.out.println( str1.equalsIgnoreCase(str2)); //true

  }

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  5. String associated with the acquisition process

  Outline

  public int length (); Get the number of characters contained in the character string which, to get the string length

  public String concat (String str); and the current parameter string concatenation string becomes the return value as a new string

  public char charAt (int index); obtaining a single character at the specified index. (0 indexed)

  public int indexOf (String str); lookup index position of the first occurrence of the parameter string of this character string in which, if there is no value -1

  1

  2

  3

  4

  5.1 length()和concat(String str)

  public static void main(String[] args) {

  // length () method, get the length of the string

  int length = "abcdefghijk".length();

  System.out.println ( "length of the string are:" + length);

  // concat (String str), string concatenation

  String str1 = "Hello";

  String str2 = "World";

  String str3 = str1.concat (str2); // str1 after splicing produces a new string, but itself unchanged str1

  // print string splicing

  System.out.println(str1);

  System.out.println(str2);

  System.out.println(str3);

  }

  / * Output:

  Length of the string is: 11

  Hello

  World

  HelloWorld

  */

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  5.2 charAt(int index)和indexOf(String str)

  public static void main(String[] args) {

  // charAt (int index), gets the specified index position of a single character

  char ch = "hello".charAt(1);

  System.out.println ( "character index position is No. 1:" + ch);

  // indexOf (String str), find the first index parameter string that appears in the original string position among

  // If not, it returns a value of -1

  String original = "helloworld!";

  int index = original.indexOf("ll0");

  System.out.println ( "first index value are:" + index); // 2

  }

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  6. The method of interception of the string

  Outline

  public String substring (int index); taken from a position aligned to the end of the string parameter and returns the new string. It is [begin, end), containing the left end to the right until

  public String substring (int begin, int end); taken chest begin start until the end of End, intermediate string. It is [begin, end), containing the left and the right is not included

  1

  2

  6.1 code shows

  public static void main(String[] args) {

  String str1 = "HelloWorld!";

  // substring (int index) method

  String str2 = str1.substring (5); // [5, end)

  System.out.println (str2); // World !, This is a new string

  //substring(int begin, int end) 方法

  String str3 = str1.substring(5, 8); // [ 5, 8 )

  System.out.println(str3); //Wor

  }

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  7. The method of converting a string

  Outline

  public char [] toCharArray (); splitting the current string to a string array as a return value

  public byte [] getBytes ();! array to obtain the current character string underlying note, not toBytes ()

  public String replace (CharSequence oldString, CharSequence newString); replace all old and new occurrence of the string into a string, then returns the result as a new replacement string

  1

  2

  3

  7.1 code shows

  public static void main(String[] args) {

  // toCharArray (); splitting the current string to a string array as a return value

  char[] chars = "hello".toCharArray();

  System.out.println(chars[0]); //h

  System.out.println (chars.length); // 5, noted that the methods generally array no parenthesis

  // byte [] getBytes (); obtain the current array of character strings bottom

  // Note! No toByte () method

  byte[] bytes = "abc".getBytes();

  for(int i = 0; i < bytes.length; i++) {

  System.out.println (bytes [i]); // 97, 98, 99, respectively, the characters of the ASCII code table values ​​abc

  }

  // replace methods, you can do some sensitive words filtered

  String str1 = "Fuck!";

  String str2 = str1.replace ( "uc", "**"); // replace ** uc into

  System.out.println(str2); //F**k!;

  }

  Zhengzhou infertility hospital: http: //yyk.39.net/zz3/zonghe/1d427.html/ Zhengzhou infertility hospital Which is good: http: //yyk.39.net/zz3/zonghe/1d427 .html / Zhengzhou infertility hospital rankings: http: //yyk.39.net/zz3/zonghe/1d427.html/

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  8. The method of dividing a character string

  Outline

  public String split (String regex); parameters in accordance with the rules, the string is divided into sections Note that when dividing point English period, the parameter can not be regex, to write! '. "" \\. "

  1

  8.1 code shows

  public static void main(String[] args) {

  // split (String regex) method delimited string

  String str1 = "AAA,BBB,CCC";

  String [] array1 = str1.split ( ","); // separated by a comma

  for(int i = 0; i < array1.length; i++) {

  System.out.println(array1[i]); // AAA BBB CCC

  }

  // Note! Argument split method is actually a regular expression

  // In other words, according to the English period. "" Be segmented, you must write "\\.", Because the period has a special meaning

  String str2 = "XXX.YYY.ZZZ";

  String[] array2 = str2.split("\\.");

  for(int i = 0; i < array2.length; i++) {

  System.out.println(array2[i]); //XXX YYY ZZZ

  }

  }

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  9. Small exercises

  9.1 Topic

  Keyboard input a string, and count the number of which various characters appear

  Types: uppercase letters, lowercase letters, numbers, other

  9.2 ideas

  Since it uses keyboard input, certainly Scanner

  Keyboard input is a string, then: String str = sc.next ();

  Define four variables, representing the number of sub-emergence of four character classes

  It requires a word string, a check word, we can use the method charAt String, and the string may be first converted to char [] array traverse

  Traversal string, the determination of the current type, and performs operations with four variables ++

  Printout of four variables, representing the number of four character classes appear

  9.3 code shows

  public static void main(String[] args) {

  // 1. Since the keyboard is used, certainly Scanner

  Scanner sc = new Scanner(System.in);

  // 2 keyboard input string, then: String str = sc.next ();

  String str = sc.next();

  // 3. Define four variables, representing the number of sub-emergence of four character classes

  int countUpper = 0;

  int countLower = 0;

  int countNumber = 0;

  int countOther = 0;

  // 4. Traversal string, the determination of the current type, and performs operations with four variables ++

  for(int i = 0; i < str.length(); i++) {

  char ch = str.charAt(i);

  if(ch >= 'A' && ch <= 'Z') {

  countUpper++;

  }else if(ch >= 'a' && ch <= 'z') {

  countLower++;

  }else if(ch >= '0' && ch <= '9') {

  countNumber++;

  }else {

  countOther++;

  }

  }

  // 5. Printout four variables, representing the number of four character classes appear

  System.out.println ( "number of capital letters:" + countUpper);

  System.out.println ( "the number of the lowercase letters:" + countLower);

  System.out.println ( "Digital number:" + countNumber);

  System.out.println ( "with other numbers:" + countOther);

  }

  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

  Haha, because the space on behalf of the end of the input, so in this code, you can not count the number of spaces

  ————————————————

  Disclaimer: This article is CSDN blogger "Fu 97" original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.

  Original link: https: //blog.csdn.net/weixin_43836046/article/details/96764238

Guess you like

Origin www.cnblogs.com/sushine1/p/11404524.html
Recommended