Java: string (bottom)

Source: Internet
Author: User

Determine if strings are equal

Comparing a String object cannot simply compare the operator "= =" because the comparison operator compares the address of a two string. Even if the contents of the two strings are the same.

Even if the contents of the two strings are the same, and the memory addresses of the two objects are different, the use of the comparison operator will still return false.

Equals () method and Equalsignorecase method

If two strings have the same character and length, the ture is returned when compared using the Equals method ().

Comparisons of strings using the Equals () method are case-sensitive, while the Equalsignorecase () method is used to compare two strings for equality if the case is ignored, and the returned result is still not of type Boolean.

We use a set of code to compare

public class Opinion {public static void main (string[] args) {
String S1=new string ("abc");
String S2=new string ("ABC");
Boolean b=s1.equals (S2);//defines whether B is the same as the Boolean type S1 and s2
Boolean b2=s1.equalsignorecase (S2);//define B2 As Boolean type S1 and S2 are equal
System.out.println (s1+ "\ T" + "equals" + "\ T" +s2+ "\ T" +b);//Print
System.out.println (s1+ "\ T" + "equalslgnorecase" + "\ T" +s2+ ":" +B2);
}} Execution result: ABC equals ABC false
ABC Equalslgnorecase Abc:true compares two strings in dictionary order CompareTo () method compares two strings in dictionary order, which is based on the Unicode values of the individual characters in the string. Compares the sequence of characters represented by this string object in dictionary order with the string sequence represented by the argument string if the sequence of strings represented by the Sting object is compared in dictionary order. If this string object is in the dictionary order before the parameter string, the result of the comparison is a negative integer, and if the string object is in the dictionary order after the parameter number string, the result is a positive integer, and if the two strings are equal, the result is 0 using the Equals () method comparison. Output public class Wordbook {public static void main (string[] args) {
String Str=new string ("B");
String Str2=new string ("a");
String Str3=new string ("C");
System.out.println (str+ "CompareTO" +str2+ "." +str.compareto (STR2));
System.out.println (str+ "CompareTo" +str3+ "." +str.compareto (STR3));
The result of the execution is: bcomparetoa.1
Bcomparetoc.-1 Letter Case Conversion: the toLowerCase () method of the string changes all characters in the string from uppercase to lowercase, and the toUpperCase () method changes the lowercase letter in the string to uppercase. toLowerCase () is converted to lowercase, touppercase () is converted to uppercase public class Upandlower {public static void main (string[] args) {
String Str=new string ("AbcDEF");//str defines a new string type
String newstr=str.tolowercase ();//convert abcdef in str to lowercase
String newstr2=str.touppercase ();//convert ABCDE in STR to uppercase
System.out.println (NEWSTR);//Print Newstr
System.out.println (NEWSTR2);//Print NEWSTR2
The result of the execution is: abcdef
ABCdef string Segmentation: Using the Split () method can be a string that splits the content by a specified split character or string and stores the split result in a string array. Public class Division {public static void main (string[] args) {
String str= "192.168.1.1";
String[] Firstarray=str.split ("\ \");
String[] Secondarray=str.split ("\ \", 2);
System.out.println ("The original value of STR is: [" +str+ "]");
SYSTEM.OUT.PRINTLN ("All partition result is:");
for (String A:firstarray) {
System.out.print ("[" +a+ "]");
}
System.out.println ();
System.out.println ("Split two times results for");
for (String A:secondarray) {
System.out.print ("[" +a+ "]");
}
System.out.println ();
}
The result of the execution is: the original value of STR is: [192.168.1.1]
The results of all partitions are:
[192] [168] [1] [1]
Split two times result to
[192] [168.1.1] format string class static Forma formatted string method for creating a formatted string The format () method has two overloaded forms public class All_ all time {
public static void Main (string[] args) {
Date Date=new date ();
String Year=string.format ("%ty", date);
String Month=string.format ("%tm", date);
String Day=string.format ("%TD", date);
String A=string.format ("%ta", date);
String Hour=string.format ("%th", date);
String Minute=string.format ("%tm", date);
String Second=string.format ("%ts", date);
System.out.println (year+ "/" +month+ "/" +day+ "" +a+ "" +hour+ ":" +minute+ ":" +second ");
}
The result is: 2018/07/05 Thursday 15:00:17 use regular expressions: Regular expressions are used to determine the majority of the statement, used to check a string when a certain format is satisfied with the judge E-mai address is legitimate is that several public class Judge {public static void Main (string[] args) {
Regular expression that defines the e-mail address to match
String regex= "\\[email protected]\\w+ (\\.\\w{2,3}) *\\.\\w{2,3}";//an e-mail address that conforms to this regular expression
String str1= "[email protected]";
String str2= "[email protected]";
String str3= "[email protected]";
if (Str1.matches (regex)) {
System.out.println (str1+ "is a valid e-mail address format");
}
if (Str2.matches (regex)) {
System.out.println (str2+ "is a valid e-mail address format");
}
if (Str3.matches (regex)) {
System.out.println (str3+ "is a valid e-mail address format");
}
}
The result of the execution is: [email protected] is a valid e-mail address format
[Email protected] is a valid e-mail address format string generator: Create a successful string, the length is fixed, the content cannot be changed and compiled. Here's how to verify the length of the string and the productivity of its string public class Jreque {public static void main (string[] args) {
String str= "";
To define the start time of an operation on a string
Long Starttime=system.currenttimemillis ();
for (int i=0;i<10000;i++) {
Str=str+i;
}
Long Endtime=system.currenttimemillis ();
Long Time=endtime-starttime;
System.out.println ("String consumption Time" +time);
StringBuilder builder=new StringBuilder ("");
Starttime=system.currenttimemillis ();
for (int j=0; j<10000;j++) {
Builder.append (j);
}
Endtime=system.currenttimemillis ();
Time=endtime-starttime;
System.out.println ("StringBuilder Consumption Time:" +time);
The result of the execution is: string consumes time 273
StringBuilder Consumption time: 1

Java: string (bottom)

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.