JAVA-based String

Source: Internet
Author: User

[Java] package com. cxy. e; public class StringTest {www.2cto.com/*** String class */public static void main (String [] args) {// String. valueOf converts various types to String type int a = 123; String aStr = String. valueOf (a); System. out. println ("================================ "); // String Length String name = "chenxiaoyang"; System. out. println (name. length (); System. out. println ("================================ "); // String comparison String aa = new String ("cxy"); String bb = new String ("cxy"); String cc = "cxy"; String dd = "cxy "; if (aa. equals (bb) {System. out. println ("the value of aa and bb is the same");} if (aa = bb) {System. out. println ("reference of aa and bb is the same");} else {System. out. println ("references of aa and bb are different");} System. out. println ("================================ "); // String is case-insensitive. String aa1 = new String ("cxy"); String bb2 = new String ("Cxy"); if (aa1.equalsIgnoreCase (bb2) {System. out. println ("the values of aa and bb are the same (case-insensitive)");} if (aa1.equals (bb2) {System. out. println ("the value of aa and bb is the same (Case Insensitive)");} else {System. out. println ("the values of aa and bb are different (Case Insensitive)");} System. out. println ("================================ "); // compare the ratio of two strings: String aa2 = new String ("123"); String bb3 = new String ("1011"); if (aa2.compareTo (bb3) <0) {System. out. println ("aa2 before aa3");} else if (aa2.compareTo (bb3) = 0) {System. out. println ("aa2 and aa3 are equal");} else if (aa2.compareTo (bb3)> 0) {System. out. println ("aa2 following aa3");} System. out. println ("================================ "); // determine the start and end of the System. out. println ("hello world ". startsWith ("hello"); System. out. println ("hello world ". endsWith ("ld"); System. out. println ("================================ "); // search for the string System. out. println ("a, B, c, d, e, f ". indexOf (","); System. out. println ("a, B, c, d, e, f ". lastIndexOf (","); System. out. println ("a, B, c, d, e, f ". lastIndexOf ("@"); System. out. println ("================================ "); // string truncation System. out. println ("I like java api ". substring (7); System. out. println ("I like java api ". substring (7,11); System. out. println ("================================ "); // replace String tempTestStr = "I like java api"; System. out. println (tempTestStr. replace ("java", "c ++"); System. out. println (tempTestStr); System. out. println ("================================ "); // remove the leading and trailing spaces System. out. println ("I like java api ". trim (); System. out. println ("================================ "); // change the case sensitivity of System. out. println ("java api ". toUpperCase (); System. out. println ("java api ". toLowerCase (); System. out. println ("================================ "); // a very useful method String [] strArray = "a, bdfsgf, c, d, e, f ". split (","); for (String one: strArray) {System. out. println (one);} System. out. println ("================================ ");}}

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.