use replaceAll and toLowerCase methods like this:

myString = myString.replaceAll(" ", "_").toLowerCase()

Answer from shift66 on Stack Overflow
๐ŸŒ
Quora
quora.com โ€บ How-do-I-replace-space-with-underscore-in-Java-without-a-replace-method
How to replace space with underscore in Java without a replace method - Quora
Answer (1 of 4): There are various replaceXXX() methods in String class, I assume youโ€™re just not allowed to use any of them. Here are some hints: 1. Java strings are immutable, so you canโ€™t change individual character in a strinf 2. Java string has a method toCharArray() to convert it ...
Discussions

java - Replace any number of consecutive underscores with a single space - Code Review Stack Exchange
I am coding the following interesting problem : Remove '_' and print with 1 space from a given string For example, for "w_e_b__services" output should be : "w e b services". Multiple unders... More on codereview.stackexchange.com
๐ŸŒ codereview.stackexchange.com
March 6, 2018
java - Convert spaces to underscores - Stack Overflow
It makes the programmer's intentions succinct and clear. "this is the player name with spaces converted to underscores", as opposed to 20 lines of code which, after someone reads through it and works out what it does, discovers it replaces spaces with underscores. More on stackoverflow.com
๐ŸŒ stackoverflow.com
txt - how to replace spaces to underscores in java - Stack Overflow
I have a text file named read.txt that says "JAVA PROGRAMMING" and i want to copy it to another file named write.txt and replace the space into underscore like this: "JAVA_PROGRAMMIN... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert all the spaces in an input to underscores
Here for this input i want to covert all the spaces to underscores Kindly guide More on forum.bubble.io
๐ŸŒ forum.bubble.io
8
0
February 21, 2023
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ core java โ€บ how to replace space with underscore in java
How to Replace Space with Underscore in Java - Java2Blog
September 17, 2022 - Use replaceAll() method to replace space with underscore in java. It is identical to replace() method, but it takes regex as argument.
๐ŸŒ
Blogger
javahungry.blogspot.com โ€บ 2023 โ€บ 05 โ€บ replace-space-with-underscore.html
Replace Space with Underscore in Java [2 ways] | Java Hungry
public class ReplaceSpaceWithUnderscore4 { public static void main(String args[]) { String str = " Be in Present "; str = str.trim().replaceAll("\\s+ ","_"); System.out.println(str); } } Output: Be_in_present That's all for today. Please mention in the comments if you have any questions related to how to replace space with an underscore in Java with examples.
๐ŸŒ
GitHub
gist.github.com โ€บ aatraiyee โ€บ c0243b22e292f3b111e3ce2ab2bd2c18
Write a Java program to replace spaces with underscores ยท GitHub
Write a Java program to replace spaces with underscores ยท This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Top answer
1 of 3
2

I think overall it is a nice implementation. It is clear and readeable. The use of constants is nice, makes it easy to refactor the code to make it more generic later on (e.g. replace with given character, not just space). Clear variable names (although I'd try and be consistent, and use current\previous, or cur/prev, most likely the former).

I have two remarks:

  • I am not a fan of the new line at the start of a function, it is too much, and makes me lose my focus.
  • I'd change the check if (currentChar != prevChar) to if (prevChar != UNDERSCORE). Although we can know from the check above that currentChar is an underscore, this makes it even more clear.

I don't really think you have what I'd see as duplication, but a possible other way to implement it would be to keep track of wether you are currently in a sequence of underscores. Example:

boolean previousWasUnderscore = false;
for (int i = 0; i < input.length(); i++) {
    final char currentChar = input.charAt(i);
    if (currentChar == UNDERSCORE) {
        if (!previousWasUnderscore ) {
            output.append(SPACE);
            previousWasUnderscore = true;
        }
    } else {
        output.append(currentChar);
        previousWasUnderscore = false;
    }
}
2 of 3
1

For generality, I would define a function that accepts the old and new delimiters as parameters, then overload it to make the underscore and space as defaults.

To avoid the special case for input.isEmpty(), I would define a boolean variable to indicate whether the previously encountered character was an underscore. You would also avoid having to re-inspect the previous character to see whether it was an underscore.

You can edit the string in place (using the same buffer for the input and output). It's slightly less cumbersome than calling the StringBuilder methods, in my opinion.

public class ReplaceUnderscore {

    public static String replace(String input) {
        return replace('_', ' ', input);
    }

    public static String replace(char oldDelim, char newDelim, String input) {
        boolean wasOldDelim = false;
        int o = 0;
        char[] buf = input.toCharArray();
        for (int i = 0; i < buf.length; i++) {
           assert(o <= i);
           if (buf[i] == oldDelim) {
               if (wasOldDelim) { continue; }
               wasOldDelim = true;
               buf[o++] = newDelim;
           } else {
               wasOldDelim = false;
               buf[o++] = buf[i];
           }
        }
        return new String(buf, 0, o);
    }
}
๐ŸŒ
Heikoevermann
heikoevermann.com โ€บ home โ€บ allgemein โ€บ java: how to replace whitespace with underscores?
Java: how to replace whitespace with underscores? | Heiko Evermann's developer notes
October 23, 2025 - So you want to replace blanks or tab characters or any other case of whitespace against underscores. How can you do that? The answer is the String method replaceAll and to use regular expressions.
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ forum_questions โ€บ 54208e6c80ff336390000948
How to substitute spaces for an underscore? | Codecademy
when "add" puts "Movie title:" title = gets.chomp.downcase sym_title = "" title_array = title.split(" ") title_array.each {|word| sym_title = sym_title + word + "_"} sym_title = sym_title.chomp('_') puts sym_title #This line is only here to check it's doing the right thing if movies[sym_title.to_sym].nil? puts "Movie rating (0-5)" rating = gets.chomp movies[sym_title.to_sym] = rating.to_i puts "The movie #{title} has been added with a rating of #{rating}." else puts "Movie is already in the database" end ... In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language. Beginner Friendly.Beginner Friendly4 Lessons4 Lessons ... Learn how to use JavaScript โ€” a powerful and flexible programming language for adding website interactivity.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-program-to-replace-the-spaces-of-a-string-with-a-specific-character
Java Program to replace the spaces of a string with a specific character - Javatpoint
Java Program to replace the spaces of a string with a specific character - Java Program to replace the spaces of a string with a specific character on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 66544852 โ€บ how-to-replace-spaces-to-underscores-in-java
txt - how to replace spaces to underscores in java - Stack Overflow
I have a text file named read.txt that says "JAVA PROGRAMMING" and i want to copy it to another file named write.txt and replace the space into underscore like this: "JAVA_PROGRAMMING". Big thanks to anyone who would help :) ... import java.io.*; import java.util.*; class Main { public static void main(String arg[]) throws Exception { FileReader fin = new FileReader("read.txt"); FileWriter fout = new FileWriter("write.txt"); int i; while ((i = fin.read()) != -1) { fout.write(i); } System.out.println("Successfully copied!"); fin.close(); fout.close(); } } ... Within your while loop you need to test for a space character.
๐ŸŒ
JMP User Community
community.jmp.com โ€บ t5 โ€บ Discussions โ€บ Substituting-a-space-for-underscore-in-a-character-string โ€บ td-p โ€บ 597222
Solved: Substituting a space for underscore in a character string - JMP User Community
June 8, 2023 - Hi, notice that Jarmo's code replaces the underscores with a space character, " ", while your code replaced the underscores with the empty string, "", which was at least part of the problem.
๐ŸŒ
PhraseFix
phrasefix.com โ€บ tools โ€บ replace-spaces
Replace All Spaces Tools - PhraseFix
Use this tool to replace any horizontal whitespace with a comma, underscore, period, dash, or any text you desire. Replace Spaces Example
๐ŸŒ
Bubble
forum.bubble.io โ€บ need help
How to convert all the spaces in an input to underscores - Need help - Bubble Forum
February 21, 2023 - Here for this input i want to covert all the spaces to underscores Kindly guide
๐ŸŒ
Reddit
reddit.com โ€บ r/regex โ€บ how to replace space with underscores using a regex in eplan?
r/regex on Reddit: How to replace space with underscores using a regex in EPLAN?
August 26, 2024 -

Hey, guys. Iโ€™m a total newbie when it comes to regex and have no idea what Iโ€™m looking at, so Iโ€™m asking for your help. How can I replace spaces with underscores using a regex in EPLAN?

Example string: "This is a test" --> "This_is_a _test"

I also have an image of something else Iโ€™ve done where I removed '&E5/' from the string so that only "011" was left.

In EPLAN:

Where there are a Source Text and Output Text, one can put RegEx expressions.

Solution:

๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ guide
freeCodeCamp Algorithm Challenge Template Guide - Guide - The freeCodeCamp Forum
August 4, 2019 - Convert the given string to a lowercase sentence with words joined by dashes. Relevant Links String global object JS Regex Resources JS String Prototype Replace JS String Prototype ToLowerCase Hint: 1 Create a regular expression for all white spaces and underscores. try to solve the problem now Hint: 2 You will also have to make everything lowercase. try to solve the problem now Hint: 3 The tricky part is getting the regular expression part to work, once you do that then just turn ...
๐ŸŒ
Statalist
statalist.org โ€บ forums โ€บ forum โ€บ general-stata-discussion โ€บ general โ€บ 1545744-how-to-replace-space-s-in-a-string-by-another-charater-like-and-underscore-_
How to replace space(s) in a string by another charater like and underscore (_)? - Statalist
April 9, 2020 - So, my question is: what code would replace space(s) in a string by another character like an underscore (_)? ... Four arguments are needed for that function, not three. ... subinstr(s1,s2,s3,n) Description: s1, where the first n occurrences in s1 of s2 have been replaced with s3 subinstr() is intended for use with only plain ASCII characters and for use by programmers who want to perform byte-based substitution.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 24572364 โ€บ replace-value-space-with-underscore-except-the-last-space โ€บ 24572413
Replace value space with underscore except the last space
You're right, .replace replaces ... in comments. ... Save this answer. ... Show activity on this post. First, trim the string, then replace the spaces....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ replacing-spaces-with-underscores-in-javascript
Replacing spaces with underscores in JavaScript | GeeksforGeeks
June 20, 2023 - Example 1: This example replaces all spaces(' ') with underscores("_") by using replace() method.
๐ŸŒ
HCL GUVI
studytonight.com โ€บ java-programs โ€บ java-program-to-replace-the-spaces-of-a-string-with-a-specific-character
HCL GUVI | Learn to code in your native language
March 11, 2021 - Comes with a built-in debugger to fix code errors. Supports JavaScript, Python, Ruby, and 20+ programming languages.Explore IDE