1. No matter how many spaces are there, one space is output:
Public class deletespace {<br/> Public static void main (string [] ARGs) throws exception {<br/> string = "new ADA dads "; <br/> system. out. println (string. replaceall ("{2,}", ""); // a function <br/> stringbuffer sb = new stringbuffer (); // implement using other methods <br/> int flag; <br/> for (INT I = 0; I <string. length (); I ++) {<br/> flag = 0; <br/> If (string. charat (I )! = '') {<Br/> Sb. append (string. charat (I); <br/>}else {<br/> flag = 1; <br/>}< br/> try {<br/> If (string. charat (I) = ''& string. charat (I + 1 )! = '') {<Br/> Sb. append (''); <br/>}< br/>} catch (exception e) {<br/> continue; <br/>}</P> <p >}< br/> system. out. println (SB); <br/>}< br/>
2. Remove spaces before and after the string:
Public static void main (string [] ARGs) {<br/> string a = "AAAA aaaa"; <br/> A =. trim (); <br/> while (. startswith ("") {<br/> A =. substring (1,. length ()). trim (); <br/>}< br/> while (. endswith ("") {<br/> A =. substring (0,. length ()-1 ). trim (); <br/>}< br/> system. out. println (a); <br/>}