Java program to replace spaces with hyphen in a string


Following program shows you how to replace spaces with hyphen in a string.
In this program we use .replace to replace spaces with hyphen

public class StringOperations4 {

	public static void main(String[] args) {

		String input = "Hello World";

		String result = input.replace(" ", "-");

		System.out.println("After replacing input string with - is: " + result);

	}
}

Output:

After replacing input string with - is: Hello-World