简体   繁体   中英

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?

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", "_");
        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

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(".", "_");

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

Try this one.

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

Try this,

        String sample = "Apple";
        StringBuilder stringBuilder = new StringBuilder();
        for(char value : sample.toCharArray())
        {
            stringBuilder.append("_");
        }
        System.out.println(stringBuilder.toString());
stringToModify.replaceAll("[a-zA-Z]","_");

You could do

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

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

Sure, I'll toss in an alternative:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM