1. String encryption
Design ideas:
Each character can be converted to an integer type, can also convert the integer type to a character type, so that we in the encryption time due to push 3 backward, so we can convert the character to shaping, and then add 3, after the operation of the variable into a character after the output, you can implement string encryption.
Program Flowchart:
Input string-"break the string into characters-" character to reshape-"reshape plus 3-" Convert to Character-"output."
Source code:import java.util.*;
Public class Zifu {
Public Static void Main (String args[]) {
Scanner Scanner = new Scanner (System. in);
System. out. println ("Please input str:");
String str = Scanner.next ();
int i;
char chars[] = Str.tochararray ();
for (i = 0;i < Str.length (); i++)
{
Chars[i] = (char) (Chars[i] + 3);
}
str = String. valueOf (chars);
System. out. println ("Please enter the encrypted string:" +str);
}
}
Program run
2.string.equals () method
The highest level parameter of the Equals () function is the object, and his source code is:
[Java] view plain copy
- Public boolean equals (Object obj) {
- return ( this = = obj);
- }
You can see that the return is a Boolean value that is equal to two objects.
equals in string has been rewritten. The source code is as follows:
[Java] view plain copy
- Public boolean equals (Object anobject) {
- if ( this = = AnObject) {
- return true;
- }
- if (AnObject instanceof String) {
- String anotherstring = (string) anobject;
- int n = value.length;
- if (n = = anotherString.value.length) {
- char v1[] = value;
- . char v2[] = Anotherstring.value;
- One. int i = 0;
- while ( n--! = 0) {
- if (v1[i]! = V2[i])
- return false;
- i++;
- 16.}
- . return true;
- 18.}
- 19.}
- return false;
21.}
"= =" Determines whether two objects are identical, i.e. content and address, while equals () compares content only, such as judging S1 and S2 content format s1.equals (S2);
3. Defragment the String class length (), CharAt (), GetChars (), replace (), toUpperCase (), toLowerCase (), Trim (), ToCharArray () usage instructions
Length () is used to find the lengths of the strings, and the return value is the length of the string.
CharAt () takes the character of the string at a location, starting with 0, as the char type
GetChars () copies the characters in this string to the target character array
Replace () replaces the element or substring in the original string. Returns the replaced string
toUpperCase () to capitalize characters in string strings
toLowerCase () to lowercase characters in string strings
Trim () The header of the stripped string is a space
ToCharArray () Converts a string to a character array
String Java question Essay