Public classlianxi{ Public Static voidMain (string[] args) {String str= "A team like a warrior can only seize the opportunity and play three games if the defense is lax, but the jazz still can't find a way to deal with the warrior."; //1. Write code to find the keyword "team", "opportunity" where the string str index position, find the string of the second "warrior" position, and output on the console intI1=str.indexof ("Team"); intI2=str.indexof ("Opportunity"); intI3=str.lastindexof ("Warrior"); Syetem.out.println (I1); Syetem.out.println (I2); SYETEM.OUT.PRINTLN (i3); //2. Define the int type variable m, which is the value of all the index values in the first question and intM=i1 +i2 +i3; //3. What is displayed on the console when I output m as a char typeSystem.out.println ((Char) m); //4. Write code implementation will use the STR string "," to split the array, and output the value of the index value of 4System.out.println (Str.split (",") [4]); //5. Write code implementation will "jazz" in the STR string instead of "warrior", "warrior" replaced with "jazz" and output the result to the console (disable replace method)String[] Newstr=str.split (""); String Temp=""; for(inti=0;i<newstr.length;i++){ if("Spyker". Equals (Newstr[i])) {Newstr[i]= "Yong"; }Else if("Yong". Equals (Newstr[i])) {Newstr[i]= "Spyker"; } temp+=Newstr[i]; } System.out.println (temp); //6. Write code to take a portion of the STR string on the console print this sentence: Warriors seize the opportunity to find a way to cope intA=str.indexof ("Warrior"); System.out.print (Str.substring (A, a+2)); intB=str.indexof ("Seize the opportunity"); System.out.print (str.substring (b, B+4)); intC=str.indexof ("Find a response"); System.out.print (Str.substring (c, C+4)); intD=str.indexof ("approach"); System.out.print (Str.substring (d, D+2)); //7. Write a piece of code, you can take out any QQ e-mail address in the QQ numberString qqemail = "[Email protected]"; System.out.println (Qqemail.substring (0, Qqemail.indexof ("@"))); String[] a= Qqemail.split ("@"); System.out.println (a[0]); }}
1 Public classlianxi{2 3 Public Static voidMain (string[] args) {4 5 //1. Use for and if to print a hollow square6 intN= 5;7 for(inti=0; i<n; i++){8 for(intj=0; j<n; J + +){9 if(I==0 | | i==4){TenSystem.out.print ("*"); One}Else{ A if(J==0 | | j==4){ -System.out.print ("*"); -}Else{ theSystem.out.print (""); - } - } - } + System.out.println (); - } + A at - //2. Print a diamond with a for loop - intN=4; - for(inti=0; i<n; i++){ - for(intj=0; j<n-i-1; J + +){ -System.out.print (""); in } - for(intk=0; k<2*i+1; k++){ toSystem.out.print ("*"); + } - System.out.println (); the } * for(inta=0; a<n-1;a++){ $ for(intb=0; b<a+1; b++){Panax NotoginsengSystem.out.print (""); - } the for(intc=0; c<5-(2*a); C++){ +System.out.print ("*"); A } the System.out.println (); + } - $ $ } -}
Processing of Java Practice strings