program to remove multiple white spaces from a string
Remove multiple extra white spaces from a string if in present in between a string
- public class WhiteSpace {
- public static String removeSpace(String string) {
- String str = "";
- int length = string.length();
- if (length <= 0) {
- return str;
- }
- boolean start = true;
- boolean first = false;
- for (int i = 0; i < length; i++) {
- char c = string.charAt(i);
- if (c == ' ') {
- if (start) {
- if(first)
- str += c;
- start = false;
- }
- }else {
- first=true;
- str += c;
- start = true;
- }
- }
- return str.trim();
- }
- public static void main(String[] args) {
- String str=" g fdf dddd dfd g sdfasdf ff";
- System.out.println(removeSpace(str));
- }
- }
Comments
Post a Comment