String Java question Essay

Source: Internet
Author: User

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

    1. Public boolean equals (Object obj) {
    2. return ( this = = obj);
    3. }

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

  1. Public boolean equals (Object anobject) {
  2. if ( this = = AnObject) {
  3. return true;
  4. }
  5. if (AnObject instanceof String) {
  6. String anotherstring = (string) anobject;
  7. int n = value.length;
  8. if (n = = anotherString.value.length) {
  9. char v1[] = value;
  10. . char v2[] = Anotherstring.value;
  11. One. int i = 0;
  12. while ( n--! = 0) {
  13. if (v1[i]! = V2[i])
  14. return false;
  15. i++;
  16. 16.}
  17. . return true;
  18. 18.}
  19. 19.}
  20. 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

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.