Java-string summary, java Summary

Source: Internet
Author: User

Java-string summary, java Summary

I have used a lot of knowledge about string Truncation in my recent work. I would like to summarize it here and hope that you can help me:
You can copy and paste the code and test it in java.
1. Delete the specified character ":", "-" in the string:

Public static void main (String [] args) {String time = "19:04:08"; String formatedata = time. replaceAll (":", ""); String formatedata1 = formatedata. replaceAll ("-", ""); System. out. println (formatedata1 );}
// Console output: 20151206 19040

2. truncate the specified characters in the string:

Public static void main (String [] args) {String time = "19:04:08"; // substring (3, time. indexOf ("08") the first parameter 3 represents the start position of the truncation (String starts from 0), and the second parameter is the end position which can be fully written as an integer String formatedata = time. substring (3, time. indexOf ("08"); System. out. println (formatedata);} // console output: 5-12-06 19: 04:

3. Check whether the string contains a certain character:

Public static void main (String [] args) {String time = "19:04:08"; boolean is = time. contains ("19"); System. out. println (is);} // console output: true

4. Check whether the string starts or ends with a specified string:

Public static void main (String [] args) {String time = "19:04:08"; // whether the String starts with "20" boolean is = time. startsWith ("20"); // whether the string ends with "08" boolean is1 = time. endsWith ("08"); System. out. println (is); System. out. println (is1);} // console output: true

5. Get the length of the string:

Public static void main (String [] args) {String time = "19:04:08"; int length = time. length (); System. out. println (length);} // console output: 19

6. Intercept one character:

Public static void main (String [] args) {String time = "19:04:08"; System. out. println (time. charAt (6);} // console output: 2

7. start and end of the same character

Public static void main (String [] args) {String time = "19:04:08"; int a = time. indexOf ("0"); int B = time. lastIndexOf ("0"); System. out. println ("position where string 0 appears for the first time" + a + ", and location where string 0 appears for the last time" + B);} // console output: position Where string 0 appears for the first time: 1, and position 17 for the last occurrence

8. connection string

Public static void main (String [] args) {int a = 42; StringBuffer sb = new StringBuffer (); String s = sb. append ("a = "). append (). append ("! "). ToString (); System. out. println (s) ;}// console output: a = 42!

9. Insert a string at a specified position

Public static void main (String [] args) {StringBuffer a = new StringBuffer ("I am James"); StringBuffer B =. insert (2, "yellow"); System. out. println (B);} // console output: I am Huang Xiaoming

10. Delete the specified character:

Public static void main (String [] args) {StringBuffer a = new StringBuffer ("abcdefghijklmn"); // Delete, from the character whose subscript is 3 to the character whose subscript is 7 StringBuffer B =. delete (3, 7); System. out. println (B);} // console output: abchijklmn

This is my summary and I hope to help you.

In addition, I have never been quite clear about the difference between String and StringBuffer. Most of them use String and almost never use StringBuffer. When should we use String and when should we use StringBuffer? Hope you can give me some advice. Thank you.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.