Hands-on brain 6, brain 6

Source: Internet
Author: User

Hands-on brain 6, brain 6

I. Ancient Rome Emperor Caesar used the following methods to encrypt Military Intelligence during the war:

Compile a program to encrypt or decrypt the English strings entered by the user using the preceding algorithm. The design idea, program flowchart, source code, and result are required.

1. Program Design Idea: Input arbitrary string ming from the keyboard, move each character backward 3, replace it with mi, use the loop algorithm, and finally output one character in sequence.

2. program flowchart:

3. Source Code:

Import java. util. role; public class My2 {public static void main (String [] args) {role in = new role (System. in); System. out. print ("Enter plaintext:"); String ming = in. nextLine (); in. close (); String r = ""; char mi; for (int I = 0; I <ming. length (); I ++) {mi = (char) (ming. charAt (I) + 3); r = r + mi;} System. out. println ("ciphertext:" + r );}}

Result:

Ii. String. equals () method, sorting String class Length (), charAt (), getChars (), replace (), toUpperCase (), toLowerCase (), trim (), toCharArray () usage instructions and Reading Notes.

1. String. equals () method

Used to compare the content of two strings. Use = to compare whether two string variables reference the same string object.

Usage;

(1) String s1 =NewString ("Hello ");

String s2 =NewString ("Hello ");

System.Out. Println (s1.equals (s2 ));

Compare the content of the initialization object of the constructor.

(2) String s3 = "Hello ";

String s4 = "Hello ";

System.Out. Println (s3.equals (s4 ));

Compares the content of two strings.

2. Length ()

Obtain the string length (the number of characters contained in the string ).

Usage:

(1), String s1 =NewString ("hello ");

System.Out. Println (s1.length (); // 6

(2), String s3 = "Hello ";

System.Out. Println (s3.length (); // 6

3. charAt ()

Obtains the characters at the specified position. The first character starts from 0.

Usage:

String s3 = "Hello ";

System. out. println (s1.charAt (1); // e

4. getChars (a, B, c, d)

Obtain the substring from the specified position and copy it to the character array. There are four parameters:

A. Start position of the copied character in the string (integer)

B. The subscript of the last character to be copied in the string plus 1 (integer)

C. Target character array (array name)

D. Place the copied characters in the starting subscript of the character array (integer number)

Usage:

String s1, output;

CharCharArray [];

S1 =NewString ("hello there ");

CharArray =New Char[5];

S1.getChars (0, 5, charArray, 0 );

Output + = "\ nThe character array is:"; // The character array is: hello

5. replace ()

Replace (char oldChar, char newChar): replace all oldChar characters in the string with newChar.

Usage:

String s1 = "aBC123 ";

System. out. println (s1.replace ('C', '0'); // replace all 'C' with '0'

Output result: aB0123

6. toUpperCase ()

Converts the (current) string to uppercase (English ).

Usage:

String s1 = "abC123 ";

System. out. println (s1.toUpperCase (); // converts the current string to uppercase.

Output result: ABC123

7. toLowerCase ()

Converts the (current) string to lowercase (English ).

Usage:

String s1 = "aBC123 ";

System. out. println (s1.toLowerCase (); // converts the current string to lowercase.

Output result: abc123

8. trim ()

Removes leading and trailing spaces from the string.

Usage: String s1 = "a B c"

System. out. println (s1.trim (); // remove leading and trailing spaces from the string

Output result: a B c

9. toCharArray ()

Converts a String object to a character array (space is a character)

Usage:

String s1 = "Java ";

Char [] s2 = s1.toCharArray ();

System. out. println (s2 );

Output result: Java

 

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.