String.Equals () How to use
Used to compare strings represented by a string of two objects
Public class stringequals {
Public Static void Main (string[] args) {
String s1=New string ("Hello");
String s2=New string ("Hello");
System. out. println (S1==S2); False
System. out. println (s1.equals (S2)); True
String s3= "Hello";
String s4= "Hello";
System. out. println (S3==S4); True
System. out. println (S3.equals (S4)); True
}
}
The object of a class cannot be compared directly with = = compared to two string objects that cannot be used to compare objects and constants
The test code for some string methods is as follows:
Package demo;
Stringmisc.java
This program demonstrates the length, charAt and GetChars
Methods of the String class.
//
Note:method GetChars requires a starting point
and ending point in the String. The starting point is the
Actual subscript from which copying starts. The ending point
is one past the subscript at which the copying ends.
Import javax.swing.*;
public class Stringmisc {
public static void Main (String args[])
{
String S1, OUTPUT,S2,S3;
Char chararray[];
S1 = new String ("Hello there");
Chararray = new char[5];
Output the string
Output = "S1:" + S1;
Test the Length method
Output + = "\nlength of S1:" + s1.length ();
Loop through the characters in S1 and display reversed
Output + = "\nthe string reversed is:";
for (int i = S1.length ()-1; I >= 0; i--)
Output + = S1.charat (i) + "";
Copy characters from string to char array
Meaning of the four parameters
1. The starting position of the copied character in the string
2. The last character to be copied in the string subscript plus 1
3. Target character array
4. The copied character is placed in the character array starting subscript
S1.getchars (0, 5, chararray, 0);
Output + = "\nthe character array is:";
for (int i = 0; i < chararray.length;i++)
Output + = chararray[I];
Test the Replace method
S2 = s1.replace ("E", "O"); Replace method is finished after the conversion is to return a new object, if you do not find a string to replace the original object
Output + = "\nthe replace method result is:" +S2;
Test the toUpperCase and Tplowercase
S2 = S1.touppercase (); toUpperCase is to capitalize all the strings
S3 = S1.tolowercase (); toLowerCase, right? All strings become lowercase
Output + = "\nthe Upper is:" +S2;
Output + = "\nthe Lower is:" +S3;
Test the trim//Remove the first space in the string
S2 = "Hello There";
S3 = "Hello There";
Output + = "\ n Add the Blank:" +s3.equals (S2);
S2 = S2.trim ();
Output + = "\ After Trim:" + s2.equals (S3);
Test the ToCharArray//Put the string in a character array
Char array[] = S1.tochararray ();
Output + = "\ntochararray:";
for (int i = 0; i < array.length;i++)
Output + = array[I];
Joptionpane.showmessagedialog (null, OUTPUT,
"Demonstrating String Class constructors",
Joptionpane.information_message);
System.exit (0);
}
}
String Common Method Test