The String class in 05-java

Source: Internet
Author: User
Tags alphabetic character

Program Design Ideas:

The first goal is to make the input string plus a number into another string, which is equivalent to encrypting the string.

The first step is to enter a string of strings type;

The second step is to transform the string into a character array;

The third step is to Fuga each word of this array, and if it exceeds the range of 26 English letters after adding three, let the letter subtract 26-3; back to the front;

' The fourth step outputs the resulting string.

Flow chart:

SOURCE program:

import Java.util.Scanner;/** Character Encryption * Xu Haojun Date: 2016.10.26*/Public class Jiami {public staticvoidMain (string[] args) {//TODO auto-generated Method StubSystem.out.print ("Please enter a string:"); Scanner input=NewScanner (System.inch ); String a= Input.next ();//Enter a string to assign a value to a                        Charb[]=New Char[100];//defines an array of characters for storing strings                       intI//for LoopSystem.out.print ("The string after encryption is:");  for(I=0;i<a.length (); i++)//length () is used to find string lengths{B[i]=(Char) (A.charat (i));//A.charat (i) is used to return the I character in a string//and is stored in a character arrayB[i]+=3;//adds three to each character in the character array, enabling encryption                                if(b[i]-' a ' >25) {//returns the beginning of an alphabetic character when it exceeds the bounds of an alphabetic character after adding threeB[i]-=26; }                     //so that the letters that add three out of bounds returnSystem.out.print (B[i]); //Output Encrypted characters           }               }}

Program results:

The String.Equals () method of the hands-on brain:

The Equals () method in the String class is used primarily to determine whether the current string is consistent with the contents of the passed-in string.

Specific implementation:

public boolean equals (Object anobject)

{

If the same object

if (this = = AnObject)

{

return true;

}

If the passed in parameter is an instance of the string class

if (anobject instanceof String)

{

String anotherstring = (string) anobject;

int n = count;//string length

if (n = = anotherstring.count)//Compare if the lengths are equal

{

Char v1[] = value;//The character of each position

Char v2[] = Anotherstring.value;

int i = offset;

int j = Anotherstring.offset;

while (n--! = 0)//For each location comparison

{

if (v1[i++]! = v2[j++])

return false;

}

return true;

}

}

return false;

}

Some common methods of string are summarized:

Length (): Gets the string length

CharAt (): Gets the character at the specified position

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

Replace (): substring substitution

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

Trim (): Remove the Kinsoku space:

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

The String class in 05-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.