05 String Usage

Source: Internet
Author: User
Tags alphabetic character

After-school Assignment 1: string encryption

    • The ancient Roman Emperor Caesar used the following methods to encrypt military information during the war:

Please write a program, using the above algorithm to encrypt or decrypt the user input of the English string requirements design ideas, program flowchart, source code, results.

Design ideas:

Input string, convert string to single character---each word multibyte 3-> connection string--string encryption output

Program Flowchart:

Source:

Tao Yujie 2016/10/28 20153152

Package character usage;

importjavax.swing.*;

Importjava.util.Scanner;

Publicclass Stringyy {

public static void Main (String args[])

{

Convert to Available string

System.out.println ("Enter an encrypted alphabetic character");

Scanner input=new Scanner (system.in);

String Mm=input.nextline ();

int N=mm.length ();

String Str1=new string ();

for (int i=0;i<n;i++)

{

Char A=mm.charat (i);//Take out a single character

A= (char) (a+3);//each character corresponds to plus 3 to get the corresponding character

str1=str1+a;//Connection

}

SYSTEM.OUT.PRINTLN ("Encrypted as:" +STR1);

}

}

Results:

Homework after Class 2: hands-on brain

Run the following sample code Stringpool.java to see its output. How do I interpret this output? What can you sum up from it?

Output Result:

True Ture false

Thinking:

Java saves a string of the same content in the same address, and the character = = determines the address and data, so S0==S1 is true. and the string + will automatically connect, so actually S2 is string s2= "HELLO", and S0==s2 is true. These are all used in the same memory space, and once new represents the request for a newer memory space, the result is that different memory locations will, of course, be shown false.

Why does the above output result? What can you sum up from it?

Output Result:

True false False True

Thinking:

S1 and S2 Reference the same character "a", because the data content is the same, save at the same address, s1==s2 result is the true,string type + = operation to get a new string object, S1==s2 to False. The string constant address is independent of the requested string object address, so s2== "AB" is false.

. Equals () determines the data content.

Please review the implementation Code of the String.Equals () method and learn how to implement it.

The implementation code mentions string s1=newstring ("Hello"), and the difference between the string s1= "Hello"; We know that the new object, even if the string data content is the same but the address is still different, and if it is a string type variable assignment, the result is different, the same data content variable address is the same.

Defragment the String class length (), CharAt (), GetChars (), replace (), toUpperCase (), toLowerCase (), Trim (), ToCharArray () usage notes

Length () : S. Length () stands for string s

charAt () : S.charat (i) presents a single I-character on behalf of S string

GetChars () :chararray =new char[5];//defines a string array

S1.getchars (0, 5, chararray,0);//The string S1 from subscript 0 to subscript 5 in the Charat array, and the copied character is labeled 0 at the beginning of the Charat.

for (int i =0; i < chararray.length;i++)

output+= chararray[i];//output characters as an array of characters

replace () : replaces a character in the original string with the specified character and gets a new string

toUpperCase () , toLowerCase (): case conversion, toLowerCase () is used to change all uppercase letters in a string to lowercase letters, and Method toUpperCase () is used to change all lowercase letters in a string to uppercase letters. The return value is a character that has been converted.

Trim () : Remove the leading and trailing spaces to get a new string.

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

05 String Usage

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.