1 ImportJava.util.Scanner;2 3 4 Public classNumber {5 6 Private StaticObject i;7 8 /*9 * The character "I" appears several times in the first question Mingrikejijavabu and outputs the result*/Ten Public Static voidMain (string[] args) { One AString r = "Imingrikejijavabi"; - - the //The first kind of interception - intA=0; - for(intJ=0;j<r.length (); j + +){ -String zhuaqu=r.substring (J, j+1); + if(Zhuaqu.equals ("I")){ -a++;} +}system.out.println ("I appears in the number of times" +a); A at - //Second Substitution -String e = r.replace ("I", "");//replace I with "empty" -System.out.println ("I appears as a number of times" + (R.length ()-e.length ())); - //the length of the character before the change minus the changed character length - in //The Third Kind of segmentation - intCount=0; toString[]find=r.split ("");//"Empty" between characters as a separator + for(String S:find) {//foreach Traversal - if(S.equals ("I")) {//The condition judgment increases from one thecount++; * } $}system.out.println ("I appears in the number of times" +count);Panax Notoginseng - the + A the + - //The second problem uses the For loop and the IF condition sentence implementation to remove the space $ $String s=NewString ("2 3 4 5"); - - //The first kind of segmentation the -String[]news=s.split ("");WuyiSystem.out.print ("2 3 4 5 Remove space for");//in the "none" between characters, note that even a space character can be output the for(String h:news) {//foreach Traversal - if(H.equals (""))//Compare Wu { - } About Else{ $ System.out.print (h); - } - }system.out.println (); - A //The second kind of interception +System.out.print ("2 3 4 5 remove space as"); the for(intI=0;i<s.length (); i++) {//Build an index -String kongge=s.substring (i, i+1);//truncate the characters between adjacent indexes $ if(Kongge.equals ("")) {//Compare the the } the Else{ the System.out.print (Kongge); - } in }system.out.println (); the the About //Third Substitution theSystem.out.println ("2 3 4 5 Remove space for" +s.replace ("", "" ")); the the + //The third question determines whether the string ends with some characters without endwith - theString str2 =NewString ("ABCFEFKL");//Create a stringBayi theSystem.out.println ("Please enter the letter to be queried");//establish scan and receive theScanner tt=NewScanner (system.in); -String chaxun=tt.nextline (); - the the if(Str2.lastindexof (Chaxun) ==str2.length ()-chaxun.length ()) { the //total length of the segment minus the length of the character segment to query equals the index value of the first character of the character segment to be queried theSystem.out.println ("str2" ends with "+chaxun+"); - } the Else{ theSystem.out.println ("STR2 does not end with" +chaxun+ "); the }94 } the}
2016/1/12 first output I number of occurrences the second problem is to use for loop and if conditional sentence to remove the string hollow lattice the third problem without endwith to implement the end character query