API

Summary of the API

Application Programming Interface (API) : Application Programming Interface

Apis in Java refer to Java classes that provide functionality in the JDK and encapsulate the underlying implementation

Keyboard entry string

Scanner class:

Next () : No more data entry when space is encountered, end mark: space, TAB key

NextLine () : Can receive data in its entirety, ending with a carriage return newline character

 

When nextInt is used in conjunction with the nextLine method, the nextLine method has no chance of keyboard entry

Suggestion: Use the next method to accept strings and integers when typing data

public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(" Please enter an integer :"); int num = sc.nextInt(); // 10 + enter newline system.out.println (" Please enter string :"); String s = sc.nextLine(); System.out.println(num); System.out.println(s); }Copy the code

The String class

Summary of the String

The String class is included in the java.lang package, so there is no need to use the package

2 The String class represents strings, and all String literals in Java programs (such as “ABC”) are implemented as instances of this class. That is, all double-quoted strings in Java programs are objects of the String class

3 Strings are immutable and their values cannot be changed after creation

Constructor of the String class

Public String() : Creates a blank String object with no content. String s1 = new String();Copy the code
Char [] CHS = {'a','b','c'}; char[] CHS = {'a','b','c'}; String s2 = new String(chs);Copy the code
Public String(String original) : create String object s3 = new String("123");Copy the code
String s = "ABC"; Creates a string object, ABC, as a direct assignmentCopy the code

Creates a distinction comparison of string objects

Created by constructor

The string object created by new allocates a memory space each time, with the same content but different address values

Create in direct assignment mode

As long as the character sequence is the same (order and case), no matter how many times it occurs in the program code, the JVM creates only one String object and maintains it in the String constant pool

(Starting with JDk7, the string constant pool is moved to the heap)

String in string constant pool:

Not exist: Created

Exists: No creation is required

String s1="abc";
String s2="ab";
String s3=s2+"c";
System.out.println(s3==s1)  //false

String s1="abc";
String s2="a"+"b"+"c";
System.out.println(s1==s2)  //true
Copy the code

When String variables are concatenated with String variables or String variables and String constants using “+”, the system automatically creates a StringBuilder object, then calls append to concatenate it, and finally calls toString to convert it toString.

Concatenation between string constants: Java has constant optimization that converts “a”+”b”+”c” to “ABC”

String comparison

String s1 = "abc"; 
String s2 = "ABC"; 
String s3 = "abc";
Copy the code

Equals: Compares the contents of strings, case sensitive

System.out.println(s1.equals(s2));   //false
System.out.println(s1.equals(s3));   //true
Copy the code

EqualsIgnoreCase: Compares string contents, ignoring case

System.out.println(s1.equalsIgnoreCase(s2));  //true
Copy the code

Traversal string

Public char charAt(int index) : returns the char value at the specified index. The index of the string also starts at 0

public static void main(String[] args) { // 1. Scanner sc = new Scanner(system.in); System.out.println(" please enter :"); String s = sc.nextLine(); For (int I = 0; for(int I = 0; i < s.length(); I ++){// I: each index of the string char c = s.char (I); System.out.println(c); }}Copy the code

Public char[] toCharArray() : Splits the current string into an array of characters and returns it

public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(" please enter :"); String s = sc.nextLine(); char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) { System.out.println(chars[i]); }}Copy the code

String interception

String subString (int index) Intercepts a String from index to the end. String is returned

String subString (int start,int end) Intercepts a String from start to end (including the header, but not the tail)

Scanner sc = new Scanner(system.in); // 1. System.out.println(" please input mobile phone number :"); String telString = sc.nextLine(); String start = telstring.substring (0,3); String end = telString.subString (7); Println (start + "****" + end); system.out. println(start + "****" + end);Copy the code

String substitution

String replace(CharSequence target, CharSequence replacement) replaces the contents of the current TARg with the contents of replacement

Scanner sc = new Scanner(system.in); // 1. System.out.println(" please enter :"); String s = sc.nextLine(); / / 2. Replace the words String result = s.r eplace (" TMD ", "* * *"); System.out.println(result); // 3.Copy the code

Cut string

String[] split(String regex, int limit) splits (String regex, int limit) according to the incoming String as a rule, regex is the regular expression separator, and splits the String into an array of strings, finally returns the array of strings,limit is the number of split

Scanner sc = new Scanner(system.in); // Scanner sc = new Scanner(system.in); System.out.println(" Please input student information :"); String stuInfo = sc.nextLine(); // stuInfo = "stuInfo "; String[] sArr = stuinfo.split (","); // System.out.println(sArr[0]); // System.out.println(sArr[1]);Copy the code

Note:., $, | and * escape character, must add \ \.

Note: multiple separator, | can be used as a hyphen.

public class Test { public static void main(String args[]) { String str = new String("Welcome-to-Runoob"); System.out.println("- separator return value :"); for (String retval: str.split("-")){ System.out.println(retval); } System.out.println(""); System.out.println("- separator set split number return value :"); for (String retval: str.split("-", 2)){ System.out.println(retval); } System.out.println(""); String str2 = new String("www.runoob.com"); System.out.println(" Escape character return value :"); for (String retval: str2.split("\\.", 3)){ System.out.println(retval); } System.out.println(""); String str3 = new String("acount=? and uu =? or n=?" ); System.out.println(" Multiple separator return values :"); for (String retval: str3.split("and|or")){ System.out.println(retval); }}}Copy the code
- Delimiter Returned value: Welcome to Runoob - Delimiter Set the number of split shares Returned value: Welcome to Runoob Escape character Returned value: WWW Runoob com Multiple delimiters returned value: acount=? uu =? n=?Copy the code

The StringBuilder class

Summary of the StringBuilder class

Overview: StringBuilder is a mutable string class. We can think of it as a container, where mutable means that the contents of a StringBuilder object are mutable

A constructor

Public StringBuilder() creates a blank mutable String object with no content. Public StringBuilder(String STR) creates a mutable String object based on the contents of the StringCopy the code
StringBuilder sb = new StringBuilder();
StringBuilder sb2 = new StringBuilder("hello");
Copy the code

Member methods commonly used by StringBuilder

append / reverse

Public StringBuilder AppEnd (any type) Adds data and returns the object itself public StringBuilder Reverse () returns the reverse character sequenceCopy the code
sb.append("hello").append("world").append("java").append(100); Sb.reverse ();Copy the code

Append returns the object itself (a C++ like reference), which can be chained or controlled by multiple StringBuilder objects

StringBuilder sb=StringBuilder("hello");
StringBuilder s=sb.append(" world");
System.out.println(s);  //hello world
Copy the code

toString()

Public StringBuilder(String s) public StringBuilder(String s String is converted to StringBuilderCopy the code
StringBuilder sb=new StringBuilder("hello");
String s = sb.toString();   //StringBuilder-->String
StringBuilder sb = new StringBuilder(s);  //String-->StringBuilder
Copy the code

StringBuilder improves efficiency principles

String concatenation:

The StringBuilder joining together:

 

Case 1: Judge the text

public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s1=sc.nextLine(); StringBuilder sb1= new StringBuilder(s1); sb1.reverse(); String sb=sb1.toString(); If (sb.equals(s1)){system.out.println (sb.equals(s1)); }else{system.out.println (" not return text "); }}Copy the code

Case 2: StringBuilder concatenates strings

Define a method that concatenates the data in an int array into a string in the specified format, calls the method, and prints the result on the console. For example, the array is int[] arr = {1,2,3}; , the output result after executing the method is: [1, 2, 3]

Public static void main(String[] args) {int[] a={1,2,3}; String s=toArryString(a); System.out.println(s); } public static String toArryString(int[] a){ StringBuilder sb=new StringBuilder(); sb.append("["); for (int i = 0; i < a.length; i++) { if(i==a.length-1){ sb.append(a[i]).append("]"); }else{ sb.append(a[i]).append(","); } } String s=sb.toString(); return s; }Copy the code