Topic:
1. Given a string, determine if the string contains a substring. If included, find all occurrences of the substring.
For example: "ABCBCBABCB34BCBD", the "BCB" substring appears in the position: 1,7,12. Strings and substrings are entered by the user
2. Given a length, randomly produce a string of that length, consisting of uppercase, lowercase letters, and numbers
Generation of random numbers in Java:
Java.util.Random r = new Java.util.Random ();
int a = R.nextint (+): Random number of a 0-99
3. Given a string consisting of numbers, such as: "1239586838923173478943890234092", statistics
Number of occurrences of each number
The answers are as follows:
Package Chp10;import Java.util.hashmap;import Java.util.map;import java.util.random;import java.util.Scanner;import Java.util.set;public class Teststring2{public static void Method_one () {Scanner s = new Scanner (system.in); System.out.println ("Please enter a parent string"); String str = S.next (); System.out.println ("Please enter a substring"); String str1 = S.next (); int stat = 0;while (true) {int i = Str.indexof (Str1,stat); if (i==-1) {break;} System.out.println (i); stat = I+str1.length ();}} public static void Method_two () {System.out.println ("Please enter a length"); Scanner s = new Scanner (system.in); int n = s.nextint (); char[] cs = new Char[n]; Random random = new random (), for (int i = 0; i < n; i++) {int a = Random.nextint (+); if (a>=0 && a<=25) {a+= 65;} else if (a>25 && a<=51) {//97-26 = 71a+=71;} else {//52-48 = 4a-=4;} Cs[i] = (char) A;} SYSTEM.OUT.PRINTLN (CS);} public static void Method_three () {Scanner Scanner = new Scanner (system.in); String string = Scanner.next (); int[] C = new Int[10];for (int i = 0; i < StrinG.length (); i++) {Char C1 = String.charat (i);//48-57int a = c1-48;//type conversion c[a]++;} for (int i = 0; i < c.length; i++) {System.out.println (i+ "occurrences:" +c[i]);} System.out.println ("========= method two =========="); String s = "12548954231351486214894562"; char[] ch = s.tochararray (); Map<character, integer> map = new Hashmap<character, integer> (); set<character> set = Map.keyset (), for (int i = 0; i < ch.length; i++) {if (Set.contains (Ch[i]))//Determine the existing char value in map {Map.put (Ch[i], Map.get (Ch[i]) +1);} Else{map.put (Ch[i], 1);//first put in map}}set<character> Set2 = Map.keyset (); for (Character Character:set2) { System.out.println (character+ "==========" +map.get (character));}} public static void Main (string[] args) {System.out.println ("the first question reads:"); Method_one (); System.out.println ("The second question is as follows:"); Method_two (); System.out.println ("The third question is as follows:"); Method_three ();}}
Java String class Exercises