编程知识 cdmana.com

My 19th day of learning Java in Shanghai Le byte

Java character string

stay Java in , Strings are treated as String Object handling of type

String Brief introduction to common methods

establish String Object method

String s1= “com”; Create a string object com, be known as s1
String s2=new String; Create an empty string object , be known as s2
String s3=new String(“com”); Create a string object com, be known as s3

 Insert picture description here

int length()、charAt(int index)、String substring(int beginIndex)、String substring(int beginIndex, int endIndex)

 Insert picture description here

package com.SH.comProj;

public class StringDemo1 {

public static void main(String[] args) {

// Define a string "JAVA Programming Basics "

String str="JAVA Programming Basics ";

// Print the length of the output string

System.out.println(" The length of the string is :"+str.length());

        //charAt(int index)

// Take out the characters ' cheng ' And the output

System.out.println(str.charAt(6));

// Take out the string " Programming Basics " And the output

System.out.println(str.substring(5));

// Take out the string " Programming " And the output

System.out.println(str.substring(5, 7));

}

}

int indexOf(int ch)、int indexOf(String str)、int lastIndexOf(String str)、int lastIndexOf(String str, formIndex)

package com.SH.comProj;

public class StringDemo2 {

public static void main(String[] args) {

// Define a string "JAVA Programming based , I like java Programming "

String str=new String("JAVA Programming based , I like java Programming ");

// To find the character 'A' First occurrence in string

System.out.println(" character 'A' First occurrence in string "+str.indexOf('A'));

// Find substring " Programming " First occurrence in string

System.out.println(" Substring " Programming " First occurrence in string "+str.indexOf(" Programming "));

// To find the character 'A' Last occurrence in string

System.out.println(" character 'A' Last occurrence in string "+str.lastIndexOf('A'));

// Find substring " Programming " Last occurrence in string

System.out.println(" Substring " Programming " Last occurrence in string "+str.lastIndexOf(" Programming "));

// In string index The value is 8 The position begins , Find substring " Programming " First occurrence

System.out.println(" In string index The value is 8 The position begins , Find substring " Programming " First occurrence "+str.indexOf(" Programming ", 8));

}

}

byte[] arrs=str.getBytes();

  • String and byte Conversion between arrays
  • Coding problem :GBK Coding and UTF-8 code
package com.SH.comProj;

import java.io.UnsupportedEncodingException;

public class StringDemo3 {

public static void main(String[] args) throws UnsupportedEncodingException {

// String and byte Conversion between arrays

// Define a string

String str=new String("JAVA Programming Basics ");

// Convert string to byte Array , And print out

byte[] arrs=str.getBytes("GBK");

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

System.out.print(arrs[i]+" ");

}

System.out.println();

// take byte Array to string

String str1=new String(arrs,"GBK");

System.out.println(str1);

}

}

== and equals The difference between ( Address and content )

Memory location in memory

 Insert picture description here

package com.SH.comProj;

public class StringDemo5 {

public static void main(String[] args) {

// == and equals Differences in methods

// Define three strings , It's all about com

String str1="imooc";

String str2="imooc";

String str3=new String("imooc");

System.out.println("str1 and str2 It's the same thing ?"+(str1.equals(str2)));

System.out.println("str1 and str3 It's the same thing ?"+(str1.equals(str3)));

System.out.println("str1 and str2 The same address ?"+(str1==str2));

System.out.println("str1 and str3 The same address ?"+(str1==str3));

}

}

String The immutability of

  • String Once the object is created , Can't modify , It's immutable
  • The so-called modification is actually to create a new object , The memory space pointed to does not change
package com.SH.comProj;

public class StringDemo6 {

public static void main(String[] args) {

//String The immutability of

//String Once the object is created , Can't modify , It's immutable

// The so-called modification is actually to create a new object , The memory space pointed to does not change

String s1="com";

String s2="hello,"+s1;

//s1 No more pointing com The memory space , It points to "hello,com"

System.out.println("s1="+s1);

System.out.println("s2="+s2);

String s3=new String("hello,com!");

System.out.println(" Substring :"+s3.substring(0,5));

System.out.println("s3="+s3);

}

}

character string StringBuilder

  • String and StringBuilder The difference between :
    – String Have no variability , and StringBuilder Do not have
    – StringBuffer and StringBuilder Class can be modified many times , And don't generate new unused objects

Differences among the three :

  • String: Immutable string ;
  • StringBuffer: Variable string 、 Low efficiency 、 Thread safety
  • StringBuilder: Variable character sequence 、 Efficient 、 Thread unsafe ;
  • Suggest :
    – When strings are frequently manipulated , Use StringBuilder

StringBuilder and StringBuffer

  • They are basically similar
  • StringBuilder Class in Java 5 It was proposed that , It and StringBuffer The biggest difference between them is StringBuilder Is not thread safe ( Can't sync access )
  • because StringBuilder Compare with StringBuffer Speed advantage , So in most cases, it is recommended to use StringBuilder class . However, when the application requires thread safety , Must be used StringBuffer class
  • StringBuffer It's thread safe ,StringBuilder There is no
  • Comparison in execution speed :StringBuilder > StringBuffer

API file

 Insert picture description here

StringBuilder Common methods of class

 Insert picture description here

package com.SH.comProj;

public class StringBuilderDemo1 {

public static void main(String[] args) {

// Define a string " Hello "

StringBuilder str=new StringBuilder(" Hello ");

// stay " Hello " Add the following , Change the string to " Hello ,com!"

   // str.append(',');

   // str.append("com!");

    //System.out.println("str="+str);

System.out.println("str="+str.append(',').append("com!"));

// Change the string to " Hello ,cOM!"

// Two ways :

//1、 Use delete Methods to remove om, Then insert OM

//System.out.println(" After replacement :"+str.delete(4, 6).insert(4, "OM"));

//2、 Use replace Method to replace

System.out.println(" After replacement :"+str.replace(4, 6, "OM"));

// In string " Hello ,cOM" Remove from " Hello " And the output

System.out.println(str.substring(0,2));

}

}

Usage method

 Insert picture description here

summary

  • String and StringBuilder
  • This paper introduces the common methods of these two classes
  • stay String This paper introduces the in == and equals() Differences in methods
  • It introduces String and StringBuilder The difference between , It's mainly immutability
  • Through the study , Learn more about java file (API) Use
  • The difference between the three :

 Insert picture description here

版权声明
本文为[HUIYL1]所创,转载请带上原文链接,感谢

Scroll to Top