- Purpose
- Master the use of class String class;
- Learn to use JDK help documentation;
- Content Experiments
1. Known string: "this is a test of java" required to do the following requirements :( source code, the results screenshot).
- The number of letters in the string s occurs statistics.
- The string neutron count the number of times the string "is" appears.
- The number of words in the string "is" appears in the statistics.
- Achieve reverse the string is output.
Experiment Code
public static void main (String [] args) { String S = " the this IS A Test of Java " ; int n-= (s.split ( " S " )) length -. . 1 ; . the System OUT .println ( " S appears the number of " + n-); int P = (s.split ( " is " )) length -. . 1 ; . the System OUT (.println " number appears substring is " + P); int COUNT = 0; char[]a=s.toCharArray(); for(int i=0;i<a.length;i++) { if(a[i]=='i') { if(a[i+1]=='s'&&a[i-1]==' ') { count++; } } } System.out.println("单词is出现的次数"+count); S1 the StringBuffer = new new the StringBuffer (S) .reverse (); . The System OUT .println ( " reverse result " + S1); }
Screenshot results
2. Write a program, using the following algorithm to encrypt or decrypt the English string input by the user. It requires source code, the results screenshot.
Source
java.util.Scanner Import; public class tainanle { public static void main (String [] args) { Scanner Z = new new Scanner (the System. in ); . the System OUT .println ( " Please enter the encrypted string " ); A String = z.nextLine (); char [] B = a.toCharArray (); int X = 0 , C = . 4 ; int J = b.length- . 1 ; char T; for(x=0,j=b.length-1;x<j;x++,j--) { t=b[x]; b[x]=b[j]; b[j]= t; } for(x=0,j=c-1;x<j;x++,j--) { t=b[x]; b[x]=b[j]; b[j]= t; } for(x=c,j=b.length-1;x<j;x++,j--) { t=b[x]; b[x]=b[j]; b[j]= t; } System.out.println(String.valueOf(b)); } }
Screenshot results
3. Given the string "ddejidsEFALDFfnef2357 3ed". The output string in the number of capital letters, lowercase letters count, the number of non-English letters.
Source
public static void main(String[] args) { String s="ddejidsEFALDFfnef2357 3ed"; char[]a=s.toCharArray(); int q=0; int w=0; int e=0; for(int i=0;i<a.length;i++) { if(a[i]>='A'&&a[i]<='Z') { q++; } The else IF (A [I]> = ' A ' && A [I] <= ' Z ' ) { W ++ ; } the else { E ++ ; } } the System. OUT .println ( " capital letters number: " + Q); the System. OUT .println ( " lowercase letters number: " + W); the System. OUT .println ( " non-English letters number: " +e); }
Screenshot results
to sum up
This week learning the string type, basic grasp of the 12 kinds of string type of book to use. Can also be used during the job some of the shortcut, the beginning do not know, check the internet to know, such as the reverse order.