String after class job

Source: Internet
Author: User

(i), string encryption

Design idea: When encrypting a string, convert the user input string into a character array, add three respectively, and finally add them to the new string for output. Decryption is the same.

Program Flowchart:

Source:

//Yu Shingxing string Encryption 20151020Importjavax.swing.*; Public classSecret { Public Static voidMain (string[] args) {String s=joptionpane.showinputdialog ("Encrypt by 1\n decrypt by 2");//user decides to encrypt and decrypt                if(S.equals ("1") ) {String S1=joptionpane.showinputdialog ("Please enter a password to encrypt"); String S3=""; Char[] S2=s1.tochararray ();//convert to character array             for(intI=0;i<s1.length (); i++) {S2[i]=(Char) (s2[i]+3);//add three in turn                if(s2[i]> ' Z ')//if the value above Z corresponds to minus{S2[i]=(Char) (s2[i]-26); } S3=s3+s2[i];//Convert to String} joptionpane.showmessagedialog (NULL, S3); }                Else if(S.equals ("2") ) {String S1=joptionpane.showinputdialog ("Please enter a password to decrypt"); String S3=""; Char[] S2=s1.tochararray ();//convert to character array             for(intI=0;i<s1.length (); i++) {S2[i]=(Char) (s2[i]-3);//minus three in turn .                if(s2[i]< ' A ')//if the value above Z corresponds to{S2[i]=(Char) (s2[i]+26); } S3=s3+s2[i];//Convert to String} joptionpane.showmessagedialog (NULL, S3); }    }}

Operation Result:

Encryption results:

Decryption results:

(b), method of collation

Source code for equal ():

 Public Booleanequals (Object anobject) {//if the same object        if( This==anobject) {            return true; }        //if the passed in parameter is an instance of the string class        if(AnObjectinstanceofString) {String anotherstring=(String) AnObject; intn = count;//string Length            if(n = = anotherstring.count)//compare lengths if they are equal            {                CharV1[] = value;//take the character of each position                CharV2[] =Anotherstring.value; inti =offset; intj =Anotherstring.offset;  while(n--! = 0)//for each location comparison                {                    if(v1[i++]! = v2[j++])                        return false; }                return true; }        }        return false;}

Length ():

length ()
Returns the length of this string. The length equals the number of Unicode code units in the string.

Designated by:
CharSequencein the interface. length
Return:
The length of the
character sequence represented by this object.

CharAt ():

charAt (int index)
Returns the value at the specified index char . The index range is from 0 to length() - 1 . The first value of a sequence is at the char index 0 , the second at the index 1 , and so on, similar to an array index.

If the value specified by the index char is a surrogate, the surrogate value is returned.

Designated by:
CharSequencein the interface. charAt
Parameters:
index- char The index of the value.
Return:
This string specifies the value at the index char . The first char value is at the index 0 .
Thrown:
IndexOutOfBoundsException -If the index argument is negative or less than the length of this string

GetChars ():

GetChars (int srcbegin,                     int srcend,                     char[] DST,                     int dstbegin)
copies the character from this string to the target character array.

The first character to copy is at the index, srcBegin and the last character to be copied is at the index srcEnd-1 (so the total number of characters to copy is srcEnd-srcBegin ). The character to be copied to dst the Subarray dstBegin starts at the index and ends at the index:

     

Parameters:
-The index of the
srcBegin first character in a string to copy.
srcEnd -the index after the last character in the string to be copied.
dst -target array.
dstBegin -The starting offset in the target array.
Thrown:
IndexOutOfBoundsException-If any one of the following is true:
  • srcBeginis negative.
  • srcBeginGreater thansrcEnd
  • srcEndis greater than the length of this string
  • dstBeginis negative
  • dstBegin+(srcEnd-srcBegin)Greater thandst.length

Replace ():

Replace (Char OldChar,                      char Newchar)
Returns a new string that is newChar obtained by replacing all occurrences of this string oldChar .

oldCharreturns a String reference to this object if it does not appear in the sequence of characters represented by this object String . Otherwise, a new object is created String that represents a sequence of characters except that all are oldChar replaced newChar with the String same sequence of characters represented by this object.

Example:

"Mesquite in your Cellar". Replace (' e ', ' o ')         returns "Mosquito in your Collar" "The War of Baronets". Replace (' R ', ' Y ')         returns "The bayonets" "sparring with a purple porpoise". Replace (' P ', ' t ')         

Parameters:
oldChar -the original character.
newChar -new characters.
Return:
a string derived from this string that replaces all of the strings in this string oldChar newChar .

toUpperCase ():,

toUpperCase ()
Use the default locale's rules to String convert all characters in this to uppercase. This method is equivalent to toUpperCase(Locale.getDefault()) .

Note: This method is relevant to the locale and can produce unpredictable results if used for strings that should be interpreted independently of the locale. Examples are programming language identifiers, protocol keys, and HTML tags. For example, "title".toUpperCase() in the Turkish (Turkish) locale "T?TLE" , where "?" is returned. is LATIN capital letters I with DOT ABOVE character. For the locale-related characters, use the to get the correct results toUpperCase(Locale.ENGLISH) .

Return:
To be converted to uppercase String .
See also:
toUpperCase(Locale)

toLowerCase ():

toLowerCase ()
Use the default locale's rules to String convert all characters in this to lowercase. This is equivalent to calling toLowerCase(Locale.getDefault()) .

Note: This method is relevant to the locale and can produce unpredictable results if used for strings that should be interpreted independently of the locale. Examples are programming language identifiers, protocol keys, and HTML tags. For example, "TITLE".toLowerCase() in the Turkish (Turkish) locale "t?tle" , where "?" is returned. is LATIN SMALL letter dotless I character. For the locale-related characters, use the to get the correct results toLowerCase(Locale.ENGLISH) .

Return:
To be converted to lowercase String .
See also:
toLowerCase(Locale)

Trim ()

Trim ()
Returns a copy of the string, ignoring leading and trailing blanks.

If this String object represents a sequence of empty characters, or if the code for the String first and last character of the character sequence represented by this object is greater than ‘\u0020‘ (the space character), a reference to this object is returned String .

Otherwise, if there is no code greater than ‘\u0020‘ the character in the string, a new object representing the empty string is created and returned String .

Otherwise, it is assumed that K is the index of the first character in the string that is greater than the code ‘\u0020‘ , andm is the index of the last character in the string that is greater than ‘\u0020‘ the code. Creates a new object that represents the result of the substring at the String end of the character in this string, starting at index K and ending at index m this.substring(k, m+1) .

This method can be used to truncate whitespace at the beginning and end of a string (as described above).

Return:
This string removes a leading and trailing blank copy, or returns this string if there are no leading and trailing blanks.

ToCharArray ():

ToCharArray ()
Converts this string to a new character array.

Return:
a newly assigned character array whose length is the length of this string, and its contents are initialized to the sequence of characters that contains this string representation.

String after class job

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.