Home > Back-end >  Inquire of Java
Inquire of Java

Time:09-25

Write a Java code in the given string to delete all the letters' b '(the last one except b, and finally a before b) after return a string,

CodePudding user response:

refer to the original poster weixin_47245288 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);
}

CodePudding user response:

 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);
}

CodePudding user response:

Scanner Scanner=new Scanner (System. In);
String s=scanner. NextLine ();
S="" + s +" ";
String [] bs=s.s plit (" b ");
If (bs. Length<4) {
System. The out. Println (s.t rim ());
} else {
S="";
Bs [0]=bs [0] + "b";
Bs [bs. Length - 2]=bs/bs. Length - 2 + "b".
for(int i=0; iS +=bs [I];
}
System.out.println(s);
}

CodePudding user response:

 
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 ());
  • Related