Common classes and methods of Java Basics (vii)

Source: Internet
Author: User

String class:

The Java.lang.String class represents the immutable sequence of Characters.

Common construction methods of the string class:

String (string original): creates a copy of a string object that is Original.

String (char[] value): creates a string object with a character array.

String (char[] value,int offset,int count): Creates a string object from the count character sequence starting with the offset entry with a single character array.

Package MyString;

public class Test {

public static void main (string[] Args) {

TODO auto-generated Method Stub

String s1= "hello";

String s2= "world";

String s3= "hello";

System.out.println (s1==s3);

S1=new String ("hello");

S2=new String ("hello");

System.out.println (s1==s2);

System.out.println (s1.equals (s2));

Char c[]={' s ', ' u ', ' n ', ' ', ' J ', ' a ', ' v ', ' a '};

String S4=new string (c);

String S5=new string (c,4,4);

System.out.println (s4);

System.out.println (s5);

}

}

Common methods of the string class (1):

Public char charAt (int index): returns the index character of the string;

public int length (): returns the length of the string;

public int indexof (string str): returns the first position of STR in a string;

public int indexof (string Str,int fromIndex): returns the first position of STR from the beginning of FromIndex in a string;

public boolean equalsignorecase (string another): compares strings with another (ignoring case);

public string Replace (char Oldchar,char newchar): replaces OldChar characters with Newchar characters in strings.

Package MyString;

public class Teststringmethod {

public static void main (string[] Args) {

TODO auto-generated Method Stub


String s1= "sun javava";

String s2= "Sun javava";

System.out.println (s1.charat (1));

System.out.println (s2.length ());

System.out.println (s1.indexof (' a '));

System.out.println (s1.indexof ("ava"));

System.out.println (s1.indexof (' a ', 7));

System.out.println (s1.equals (s2));

System.out.println (s1.equalsignorecase (s2));

String s= "i am a programmer, I am learning java";

String sr=s.replace (' me ', ' You ');

String si= "[ip:10]";

String sr1=si.replace (' [', ');

String sr2=sr1.replace ('] ', ');

System.out.println (sr);

System.out.println (sr1);

System.out.println (sr2);

}

}

Common methods of String class (2)

public boolean StartsWith (string prefix): determines whether a string begins with a prefix string;

public boolean EndsWith (string suffix): determines whether the string ends with a prefix string;

public string toUpperCase (): returns a string that is the uppercase form of the string;

public string toLowerCase (): returns a string that is the lowercase form of the string;

public string substring (int beginindex): returns the substring from the beginning of Beginindex to the end of the string;

public string substring (int beginindex,int endIndex): returns the substring from the beginning of Beginindex to the end of endIndex;

public string Trim (): returns the string after which the string is stripped of the beginning and trailing Spaces.

Package MyString;

public class Teststringmethodo {

public static void main (string[] Args) {

TODO auto-generated Method Stub

String s= "Welcome to Java world!";

String s1= "sun java";

String s2= "[ip1:10]";

System.out.println (s.startswith ("Welcome"));

System.out.println (s.endswith ("orld"));

String Sl=s.tolowercase ();

String Su=s.touppercase ();

System.out.println (sL);

System.out.println (sU);

String subs=s.substring (11);

String subs1=s.substring (0, 6);

String subs2=s.substring (0, 7);

System.out.println (subS);

System.out.println (subS1);

System.out.println (subS2);

String Sp=s1.trim ();

String s3=s2.replace (' [', ');

String s4=s3.replace ('] ', ');

String S5=s4.trim ();

System.out.println (sp);

System.out.println (s4);

System.out.println (s5);

}

}


This article is from the "one step, one step" blog, please be sure to keep this source http://summerflowers.blog.51cto.com/5202033/1921727

Common classes and methods of Java Basics (vii)

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.