Java Job 4

Source: Internet
Author: User

(a) Please review the implementation Code of the String.Equals () method, and learn how to implement it. (Posted on blog assignments)

(ii) collating the Length (),charAt (), GetChars (),replace ( ), touppercase (), toLowerCase (),trim (),tochararray ( ) instructions for use

Length () : Get string length

String S1 = "Welcome to Java";

System.out.println ("s1 ' s length is:" + s1.length ());

Operation Result:

S1 ' s length is:15

charAt () : gets the character at the specified position

String S1 = "Welcome to Java";

System.out.println (S1.substring (4));

System.out.println (S1.substring (2, 7));

Operation Result:

Ome to Java

Lcome

GetChars () : gets the substring copied from the specified position into the character array (it has four parameters, as described in the example)

Char[] S1 = {' I ', ' ', ' l ', ' o ', ' V ', ' e ', ', ' h ', ' e ', ' R ', '! '};/ /s1=i Love her!

String s2 = new String ("you!"); S2.getchars (0,3,s1,7); S1=i Love you!

System.out.println (S1);

Running result: I love you!

replace () : substring substitution

String s = "Welcome to Java";

System.out.println (S.replace (' e ', ' m '));

System.out.println (S.replacefirst ("E", "AB"));

System.out.println ("Eleleledsafsdfhie". ReplaceAll ("El", "WML"));

Operation Result:

Wmlcomm to Java
Wablcome to Java
Wmlwmlwmledsafsdfhie

toUpperCase () , toLowerCase () : Uppercase and lowercase conversions

String s = "Welcome to Java";

System.out.println ("S.touppercase ():" +s.touppercase ()); S.touppercase (): WELCOME to JAVA

System.out.println ("S.tolowercase ():" +s.tolowercase ()); S.tolowercase (): Welcome to Java

Trim () : Remove the Kinsoku space:

String s = "Weer ewre";

System.out.println (S.trim ()); Weer ewre

ToCharArray () : Converts a String object to a character array

Char[] S1 = {' I ', ' ', ' l ', ' o ', ' V ', ' e ', ', ' h ', ' e ', ' R ', '! '};/ /s1=i Love her!

String s2 = new String ("you!"); S2.getchars (0,3,s1,7); S1=i Love you!

System.out.println (S1);

Operation Result:

I Love you!

(iii) String Encryption

Design Ideas : Use the Charat () function to get each character in a string, encrypt when all characters change the cost character to the last three bits of the character in the alphabet, but XYZ becomes ABC, and when decrypted all characters change the cost character to the first three digits of the alphabet, but ABC becomes XYZ.

Source code:

Results:



Java Job 4

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.