Java implements a comparison of two strings

Source: Internet
Author: User
Tags stringbuffer

The CompareTo method is to compare the dictionary order of two strings
The order in the dictionary, such as "ABCD" in front of "acdb"
In addition, you should first convert the string to int and then compare
You can compare two integers in the following ways
public int max (int m,int N)
{
if (m>n)
return m;
else if (n>m)
return n;
else return 0;//0 when equal
}
can use int a=integer.parseint ("20");
To convert the string 20 to integer 20 and give a

--__---------------------------------------------------------------------------------------------------------The string comparison 1equals in Java is used to compare strings for equality, = = cannot be used as a comparison of strings. If you want to compare sizes with CompareTo (string), it is the size of each character of the string that is compared sequentially Reply: Java string comparison 2Lsj_smile (I Laugh)
I would like to know if equals is more than the object and the ratio, such as:
String s1= "Hello";
String s2= "Hello";
String S3=new string ("Hello");
S1.eauals ("Hello");
S1.equals (S2)//?
Reply: Java string comparison 3Equals comparison values are the same
= = Comparison is the same referenceReply: Java string comparison 4= =: Compare two strings to the same string object
equals: Compares the contents of two strings to be the same.Reply: Java string comparison 5Object comparisons must be made with the equals
= = Only compares two data's address, namely these two is not the same thing ...
You are one string and another string = =, and absolutely not equal.

Because they are two ...

Reply: Java string comparison 6Equals is a value comparison, = = is reference if you do not care about the said can be considered one-sided correct,
Because, for example, a value comparison of basic types such as int, Boolean, will be used = =

Is there anyone else who thought that if it's not a string object, what is the equals comparison? For example: List, iterator, Hashtable ...

Reply: Java string comparison 7Comparing objects with equals compares values, in fact, if two objects cannot be compared to equals, the compilation simply cannot pass. In some cases, such as double and float, the comparison result is always false
= = = Comparison is the same reference (type), so s1== "" is trueReply: Java string comparison 8Please determine if it is correct:
String s1= "Hello";
String s2= "Hello";
String S3=new string ("Hello");
S1.eauals ("Hello");//true?
S1.equals (s2);//true?
S1==s2//false?
s3== "Hello"//false?
Also want to ask the basic data type how to determine whether equality, = = whether can be used in the cardinality class:
int i=1; if (i==1) {}
If not, will I be packaged into an integer class and then use equals
Reply: Java string comparison 9When S1,s2 is a string, s1==s2 is wrong to write this. To determine whether they are equal must use equals
= = for judging int, char and so on. int i=1; if (i==1) {} This is the correct use.
Reply: A string comparison in Java1, simple data types can not be simply as an object, compared to the size of the value must use "= =";
2, when using the parcel class, such as Integer i = new integer (1);
Integer j = new Integer (1);
I==j is the comparison of two objects is the same reference, that is to say whether it refers to the same object, the result is obviously false;
True if comparison is compared with equals if the values are the same;
3, the comparison of objects to remove the StringBuffer class, the results are the same as the 2nd (String is the same as the 2nd);
4,stringbuffer class two invocation methods return false;
5, to say a little, the story upstairs is wrong;reply: String comparisons in JavaUpstairs, int and integer are different. If you declare the variable to be int, you cannot use equals. What's wrong with me?Reply: string comparison in JavaOh, I am saying that your s1==s2 can be compared, whether it is the same object

_____----------------------------------------------------------------------------------------------------

string Comparisons in Java why not use "= ="If you say = = = is the comparison of the address pointer, he kicked you, huh?
Java is a so-called no pointer to Oh,
It should be said that = = = is compared between two objects in the same storage unit, it is more in line with the Java statement OH
And Equals is the same as comparing the contents of two objects.
So can not say = = can not be compared, but more special. See the following example: String str1 =new string ("a");
String str2 =new string ("a");
STR1==STR2 to compare two reference addresses for equality and return false
Str1.equals (STR2) is the content of the comparison two objects, and the return value is true. ----------------------------------------------------------------------------------about String (string) [p>stringis a sequence of characters enclosed in double quotes, in the Java language where string data is actually implemented by a string class. Java string classes fall into two categories: one is the invariant string that is not changed in the program, and the second class is a variable string that is changed in length in the program. In order to store and maintain these two types of strings, the Java environment providesStringAndStringBufferTwo classes.
first, create a string
Example: String str= new ("This is a String");

or string str= "This is a string";

Second, get the information about the string object
1. By callingLength ()method to get the length of the string.
Cases:

String Str= "This is a string";
int Len =str.length ();

2.StringBuffer Class ofcapacity ()Method and the String classLength ()The method is similar, but she tests the size of the memory space allocated to StringBuffer, rather than the memory space currently being used.
3. If you want to determine the position of a specified character or substring in a string at a given string, you can use theindexOf ()AndLastIndexOf ()Method.

String Str= "This is a string";
Int index1 =str.indexof ("i");   index=2
Int index2=str.indexof (' i ', index+1);   Index2=5
Int index3=str.lastindexof ("I");   Index3=15
Int index4=str.indexof ("String");  index4=10


comparison and operation of String objects
1. Comparison of String objects
The Equals () method of the string class is used to determine whether two strings are equal.

String Str= "This is a string";
Boolean Result=str.equals ("This is another String");
Result=false


2. Access to a String object
A, methodcharAt ()The character used to get the specified position.

String Str= "This is a string";
Char Chr=str.charat (3); Chr= "I"


B, methodGetChars ()Used to get part of a string of strings

public void GetChars (int srcbegin,int srcend,char[]dst,int dstbegin)
String Str= "This is a string";
Char chr =new char[10];
Str.getchars (5,12,chr,0);  Chr= "is a St"


CsubString ()is another way to extract a string, which specifies where to start extracting the string and where to end it.
3. Action string
Areplace ()method to replace a character in a string with another character.

String Str= "This is a string";
String str1=str.replace (' t ', ' t '); Str1= "This is a String"


The B, concat () method can combine two strings into a single string.

String Str= "This is a string";
String str1=str.concat ("Test"); Str1= "This is a String Test"


CtoUpperCase ()AndtoLowerCase ()method to implement a string-case conversion, respectively.

String Str= "This is A string";
String str1=str.tolowercase (); Str1= "This is a string";


The D, Trim () method removes the spaces at the beginning and end of the string.

String str= "This is a string   ";
String Str1=str.trim ();   Str1= "This is a String"


E, String class provides static methodsvalueof (), which converts any type of data object to a string. Such as

System.out.println (string,valueof (MATH,PI));


iv. modifying variable strings
The StringBuffer class provides 3 methods for modifying a variable string, inserting and changing the character of a position in the middle of the string.
1. Append after string: WithAppend ()method to add a variety of objects to a string.
2. Inserting in the middle of a string:Insert ()Method. Cases

StringBuffer str=new StringBuffer ("This is a String");
Str.insert (9, "test");
System.out.println (Str.tostring ());


This code is output as: This is a test String
3. Change the character of a position, usingSetcharat ()Method.

Related Article

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.