James D'Amico
James D'Amico

Reputation: 39

How do I replace all the letters in a string with another character using Java?

I want to be able to replace all the letters in a string with an underscore character.

So for example, lets say that my string is made up of the alpha characters: "Apple". How would I convert that into five underscores, because apple has five characters (letters) in it?

Upvotes: 0

Views: 9437

Answers (11)

poohdedoo
poohdedoo

Reputation: 1268

What about this

  public static void main(String args[]) {
    String word="apple";

        for(int i=0;i<word.length();i++) {
            word = word.replace(word.charAt(i),'_');
        }
        System.out.println(word);
}

Upvotes: 0

jozxyqk
jozxyqk

Reputation: 17256

Why not ignore the "replace" idea and simply create a new string with the same number of underscores...

String input = "Apple";

String output = new String(new char[input .length()]).replace("\0", "_");
//or
String output2 = StringUtils.repeat("_", input .length());

largely from here.

As many others have said, replaceAll is probably the way to go if you don't want to include whitespace. For this you don't need the full power of regex but unless the string is absolutely huge it certainly wouldn't hurt.

//replaces all non-whitespace with "_"
String output3 = input.replaceAll("\S", "_");

Upvotes: 2

Woody
Woody

Reputation: 939

        String content = "apple";
        String replaceContent = "";
        for (int i = 0; i < content.length(); i++)
        {
            replaceContent = replaceContent + content.replaceAll("^\\p{L}+(?: \\p{L}+)*$", "_");
        }

        System.out.println(replaceContent);

Use Regular Expression

Regarding \p{L}: Refer Unicode Regular Expressions

Upvotes: 1

Ryan Stewart
Ryan Stewart

Reputation: 128769

Or here's a nice, recursive approach:

public static void main(String[] args) {
    System.out.println(underscores("Apple"));
}

public static String underscores(String input) {
    if (input.isEmpty()) return "";
    else return "_" + underscores(input.substring(1));
}

Upvotes: 0

Ryan Stewart
Ryan Stewart

Reputation: 128769

Sure, I'll toss in an alternative:

String input = "Apple";
char[] newCharacters = new char[input.length()];
Arrays.fill(newCharacters, '_');
String underscores = new String(newCharacters);

Upvotes: 0

Trying
Trying

Reputation: 14278

str.replaceAll("[a-zA-Z]","_");


    String str="stackoverflow";
    StringBuilder builder=new StringBuilder();
    for(int i=0;i<str.length();i++){
        builder.append('_');
    }
    System.out.println(builder.toString());

Upvotes: 0

Reimeus
Reimeus

Reputation: 159754

You could do

int length = "Apple".length();
String underscores = new String(new char[length]).replace("\0", "_");

Upvotes: 0

Francis
Francis

Reputation: 1090

You can use the String.replaceAll() method.

To replace all letters:

String originalString = "abcde1234";
String underscoreString = originalString.replaceAll("[a-zA-Z]","_");

If you meant all characters:

String originalString = "abcde1234";
String underscoreString = originalString .replaceAll(".", "_");

Upvotes: 3

Justin Lessard
Justin Lessard

Reputation: 11871

stringToModify.replaceAll("[a-zA-Z]","_");

Upvotes: 0

newuser
newuser

Reputation: 8466

Try this,

        String sample = "Apple";
        StringBuilder stringBuilder = new StringBuilder();
        for(char value : sample.toCharArray())
        {
            stringBuilder.append("_");
        }
        System.out.println(stringBuilder.toString());

Upvotes: 0

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

Try this one.

String str = "Apple";
str = str.replaceAll(".", "_");
System.out.println(str);

Upvotes: 0

Related Questions