1. Please review the implementation Code of the String.Equals () method and learn how to implement it.
public boolean equals (Object anobject) {
if (this = = AnObject) {
return true;
}
if (anobject instanceof String) {//string is a reference data type and itself is a string object, first determine if AnObject is an instance of the string class, if it is running the statement below
String anotherstring = (string) anobject; This is the AnObject object that is passed in here, assigned to anotherstring (requires type conversion)
int n = count; This count is the length of the original string
if (n = = Anotherstring.count) {//String length compared to the length of the string to be compared, the length is different than the string content
Char v1[] = value; Storing the original string as a character in an array
Char v2[] = Anotherstring.value; To compare strings into arrays as characters
int i = offset; Array subscript
int j = Anotherstring.offset; Array subscript
while (n--! = 0) {//iterate over array, compare array elements for same
if (v1[i++]! = v2[j++])//return False if there is a difference during traversal
return false;
}
return true;
}
}
return false;
}
Instructions for using the 2.string class
1.Length (): Gets the current string length
2.charAt (int index): Gets the character of the current string object subscript index at 3.getChars (): Gets the substring copied from the specified position to the character array parameter: int srcbegin,int srcend,char[] Dst,int Dstbeginsrcbegin-The index of the first character in a string to copy.
Srcend-the index after the last character in the string to copy.
DST-Destination Array.
Dstbegin-The starting offset in the target array. 4.replace (char Ch1,char CH2): Replaces the character ch1 of a string with a string ch2.5.touppercase (): Converts lowercase characters in a string to uppercase characters 6.toLowerCase () : Converts uppercase characters in a string to lowercase characters 7.trim (): Removes the leading and trailing spaces, and the trim removal process is from outside to inside until a non-whitespace character is encountered, so that no matter how many consecutive whitespace characters are removed before and after. 8.toCharArray (): Converts a String object to a character array. 3. The ancient Roman Emperor Caesar used the following methods to encrypt military intelligence during the war, write a program that uses the above algorithm to encrypt or decrypt the user-entered English string: Design idea: Convert to char array, ASCII code moves backwards three bits. Program Flowchart: Program source code:
Import Javax.swing.JOptionPane;
public class Kaisa {
public static void Main (string[] args)
{
String s;
S =joptionpane.showinputdialog ("Please enter clear text");
Char Ch[]=s.tochararray ();
for (int i=0;i<s.length (); i++)
{
if (ch[i]== ' X ' | | ch[i]== ' Y ' | | ch[i]== ' Z ')
{
Ch[i]= (char) (CH[I]-23);
}
Else
{
Ch[i]= (char) (CH[I]+3);
}
}
Joptionpane.showmessagedialog (NULL, "Ciphertext:" +string.valueof (CH));
}
Validation results:
String after class job