This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging

⭐ August more text challenge day 8 ⭐, review and consolidate Java😁 with friends

Code Mantis shrimp is a sand sculpture and interesting young boy, like most friends like listening to music, games, of course, in addition to the interest of writing, Emm… There is still a long time to go. Let’s work hard together 🌈

Welcome friends to pay attention to my public number: JavaCodes, although the name with Java, but the scope of more than Java field oh 😁, look forward to your attention ❀

The original link🌈Java from entry to grave ⭐ study notes ⭐ (1) String Common methods summary 😊 (small white essential knowledge!!

Self-recommendation, to recommend their own column 😁, welcome friends to collect attention 😊

MybatisPlus column

App Crawler Column

PC crawler column

Big factory interview questions column

Strings are widely used in Java programming. In Java, strings are objects. Java provides the String class to create and manipulate strings.

1. The charAt() method

char c = "Mantis shrimp".charAt(1);
System.out.println(c);
Copy the code

“Mantis shrimp” is an object. You can use a point as long as it’s an object.


2. The compareTo() method

int result1 = "abc".compareTo("abc");
System.out.println(result1);

int result2 = "abc".compareTo("abd");
System.out.println(result2);

int result3 = "abd".compareTo("abc");
System.out.println(result3);
Copy the code

= =conclusion= =

  1. If the argument string is equal to this string, return the value 0;
  2. If the string is less than the string argument, return a value less than 0;
  3. If this string is greater than the string argument, a value greater than 0 is returned.

The return value is an integer, it is first to compare the size of the corresponding character (ASCII), = = if the first character and the parameters of the first character, end of comparison, the difference between the return to their value = = = = if the first character is equal to the parameters of the first character, with the second character and parameter of the second character comparison = =, And so on until one of the characters being compared or compared ends.

System.out.println("abc".compareTo("cba"));
Copy the code

Compare the first string, a-c=-2, and return -2


3, contains() method

== is simply to determine whether the preceding string contains the following substring ==

System.out.println("HelloWorld!".contains("Wor"));
System.out.println("http://www.baidu.com".contains("https"));
Copy the code


4, endsWith() and startsWith() methods

System.out.println("text.txt".endsWith("txt"));
System.out.println("text.txt".endsWith("java"));
System.out.println("text.txt".startsWith("text"));
System.out.println("text.txt".startsWith("test"));
Copy the code

System.out.println("text.txt".startsWith("text".0));
System.out.println("text.txt".startsWith("test".1));
Copy the code


The difference between compareTo() and equals()

==equals() Consider case ==

System.out.println("abc".equals("abc"));
System.out.println("abc".equals("aBc"));
System.out.println("abc".equals("bbb"));
Copy the code

Return true if the given object is equal to the string; Otherwise return false.

**==compareTo() returns the difference between the two, i.e. returns a number; Equals is simpler. It only returns true or false.

Finally, both compareTo() and equals() can determine other basic data types, such as Integer. The Java source code overloads both methods to automatically match methods based on the type of the argument. πŸ™‚ this kind of judgment. = = * *


EqualsIgnoreCase () method

==equalsIgnoreCase() Ignores case ==

System.out.println("abc".equalsIgnoreCase("abc"));
System.out.println("abc".equalsIgnoreCase("ABc"));
System.out.println("abc".equalsIgnoreCase("bbb"));
Copy the code


7. The getBytes() method

byte[] bytes = "abcde".getBytes();
for (byte b:bytes){
 System.out.println(b);
}
Copy the code


8. The indexOf () and lastIndexOf() methods

System.out.println("abcdefghygklmn".indexOf("a"));
System.out.println("abcdefghygklmn".indexOf("def"));
System.out.println("abcdefghygklmn".indexOf("l"));
Copy the code

System.out.println("abcdefgaabbcc".lastIndexOf("a"));
System.out.println("abcdefgaabbcc".lastIndexOf("c"));
Copy the code


9. IsEmpty () method

System.out.println("".isEmpty());
System.out.println("aaa".isEmpty());
Copy the code


10. Replace () method

String text = "aaabbbccc";
System.out.println(text.replace("a"."d"));

String param = "Name = Mantis shrimp &age=18&address= Pond";
System.out.println(param.replace("&".":"));
Copy the code


11, split() method

You can also split them by strings

String str = "2020-08-14";
String[] splits = str.split("-");
	for (String s: splits) {
	    System.out.println(s);
 }

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

 String param = "Name = Mantis shrimp &age=18&address= Pond";
 String[] split = param.split("&");
	for (String s: split) {
	    System.out.println(s);
 }
Copy the code


12. Substring () method

  1. BeginIndex — Start index (including), index starting from 0.

  2. EndIndex — endIndex (not included).

String text = "http://www.baidu.com";
System.out.println(text.substring(7));
System.out.println(text.substring(0.7));
Copy the code


13. ToCharArray () method

char[] chars = "Favorite shrimp, mantis shrimp.".toCharArray();
for (char ch:chars){
       System.out.println(ch);
}
Copy the code


14. ToLowerCase () and toUpperCase() methods

String text = "aaAA";
System.out.println(text.toLowerCase());
System.out.println(text.toUpperCase());
Copy the code


15, trim() method

== Remove whitespace before and after the string ==

String text = " hello world! ";
System.out.println(text.trim());
Copy the code


16. The Valueof() method

double d = 1111.11;
boolean b = false;
long l = 123456789;
char[] arr = {'w'.'o'.'r'.'l'.'d'};

System.out.println(Return value: + String.valueOf(d) );
System.out.println(Return value: + String.valueOf(b) );
System.out.println(Return value: + String.valueOf(l) );
System.out.println(Return value: + String.valueOf(arr) );
Copy the code

The Valueof() layer calls the toString() method

When the argument is an object

  1. When the toString() method is not overridden
public class Main {

    public static void main(String[] args) {
        String s = String.valueOf(newPerson()); System.out.println(s); }}class Person{}Copy the code

== Print memory address ==

  1. Overrides the toString() method
public class Main {

    public static void main(String[] args) {
        String s = String.valueOf(newPerson()); System.out.println(s); }}class Person{
    @Override
    public String toString(a) {
        return "I'm a mantis shrimp."; }}Copy the code

== Prints data returned by toString() ==


16.1, system.out.println () low-level call

Object o = new Object();
System.out.println(o);
Copy the code

Call the valueOf ()

Call the toString ()

In fact, the system.out.println () method converts any data to a string and then prints it.





The last

I am aCode pipi shrimpI am a mantis shrimp lover who loves to share knowledge. I will keep updating my blog in the future. I look forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == a key three! ==, thanks for your support, see you next time ~~~