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

}

No comments:

Post a Comment

How ChatGPT can Benefit Coding: Your Guide to Leveraging an AI Language Model

 Introduction: Hello, coders! Welcome to this blog post on how ChatGPT, an AI language model, can benefit your coding skills and projects. A...