Monday, July 15, 2013

how to replace 2 or more spaces with single space in string using Java

package com.swain;

public class StringUtil {
    public static void main(String args[]) {
        String before = " Himanshu      Sekhar           Swain  ";
        System.out.println("before: " + before);
        String after = before.trim().replaceAll(" +", " ");
        System.out.println("after: " + after);
    }

}

Java Quick Program

1. Write a Code to generate Random numbers public class GenerateRandomNumbers { public static void main(String[] args) { System.out...