Java Character replace space with dot in String

Question

We would like to replace space character with dot in a String.

public class Main {
    public static void main(String[] arguments) {
        String s = "this is a test test test";
        //your code here 
    }
}


public class Main {
    public static void main(String[] arguments) {
        String s = "this is a test test test";
        char[] mfl = s.toCharArray();
        for (int dex = 0; dex < mfl.length; dex++) {
            char current = mfl[dex];
            if (current != ' ') {
                System.out.print(current);
            } else {
                System.out.print('.');
            }
        }
        System.out.println();
    }
}

Note

We converted the String to char array first.

Then we used if statement to check the char value.




PreviousNext

Related