Time:09-25
CodePudding user response:
write a Java code in the given string to delete all the letter 'b' (the last one except b and the last one before b) return a string after
Public static void main (String [] args) {//TODO Auto - generated method stub Scanner sc=new Scanner (System. In); String STR=sc. Next (); System. The out. Println (STR); String sub1=STR. The substring (0, STR., length () - 3); String sub2=STR. The substring (STR) length () - 2); STR=sub1. Replace (" b ", "") + sub2; System. The out. Println (STR); }
String STR="asdfbfdfsdnnsbn25n235nsascb"; STR="abbssbdfsdfbdfbb"; String pattern="(. *) [^ b] * (b) (b [^ b] *)". Pattern r=Pattern.compile(pattern); Matcher m=r.matcher(str); If (m. ind ()) {System. The out. Println (m.g roup (1). The replaceAll (" b ", "") + m.g roup (2) + m.g roup (3)); } else {System. The out. Println (STR); }
String STR="BCBDBSB"; StringBuilder sb=new StringBuilder(); Sb. Append ((STR) charAt (0))) Append (STR. The substring (1, STR., length () - 1). The replaceAll (" b ", "")) Append (STR) charAt (STR) length () - 1)); System. The out. Println (sb. ToString ());
Page link:https//www.codepudding.com/Backend/31020.html