1

Can't figure out how to replace a string with a character in Java. I wanted a function like this:

replace(string, char)

but there's not a function like this in Java.

String    str = str.replace("word",  '\u0082');
String    str = str.replace("word",  (char)130);

How do I go about this?

2
  • 4
    Why not just make the char a String? Example: str.replace("word", "" + '\u0130'); Commented May 29, 2015 at 2:51
  • 1
    @40-love ,James Taylor is right. You can do task my converting char to String. Or If you have special requirement then please let me know your use case .. Commented May 29, 2015 at 3:22

5 Answers 5

1

Use a string as the replacement that happens to be only a single character in length:

String original = "words";
String replaced = original.replace("word", "\u0130");

The replaced instance will be equivalent to "İs".

Note also that, from your question, '\u0130' and (char)130 are not the same characters. The \u syntax uses hexadecimal and your cast is using decimal notation.

Sign up to request clarification or add additional context in comments.

5 Comments

That will work for this particular instance, but if he wants to re-use this code and might not know which character is going to be the replacement, then this fails.
Oops. Thanks for the note about hexadecimal.
It works if every place the OP wants to use it will always be a character literal. Since most user- and database-sourced input is going to come in as a String anyway, that assumption is not entirely unlikely. If the replacement is going to be a hardcoded literal, I feel that the Java API-provided method on String is preferable to creating a new method in some utility class.
You're right. But why didn't you choose to pass in the original string as the first parameter? Won't that always be safe anyway?
@MatthewC I didn't pass in the original string because my assumption is that OP wants to replace a portion of the original string and not the entire value. In my example, the substring "word" is replaced by a single character, leaving the remaining characters alone. I'm not going to edit my post at this time because I have nothing to add to it. Thank you for considering to change your vote; next time, please don't be so quick to downvote other answers that may be helpful even if you don't think they're perfect.
1

Very simply:

String orginal = "asdf";
char replacement = 'z';
orginal = original.replace(original, replacement+"");

8 Comments

This method does nothing; the caller still has the unchanged original String.
You're completely correct, I've misplaced my code from other programming languages here :p Its now fixed :)
And if that's what the OP really wants to do, how is all of that "cleaner" code than simply original = "" + replacement; without a helper method?
That's why I've changed it to.
Except that String.replace(String, char) does not exist in the Java API.
|
1

You asked for a "function" in Java, you can allways create a method like this

public static String replace (String text, char old, char n){
    return text.replace(old, n);
}

Then you can call this method as you want

String a = replace("ae", 'e', '3');

In this case the method will return a String with a3 as value, but you can replace not only a char by another, you can replace a String with multiple characters in the same way

public static String replace (String text, String old, String n){
    return text.replace(old, n);
}

Then you call this method like this

String a = replace("aes", "es", "rmy");

The result will be a String with army as value

7 Comments

This will not replace the entire string. As taken from Oracle Docs: "replace: Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar." I believe he was asking for a method which replaces a String with a character. Not replaces certain characters in a String with a different character.
I see now that your first bit of code will replace the entire String with a new entire String. However, I believe it looks obfuscated with two of the +"" in it.
Yes, this will replace all ocurrences of the char in the second parameter for the new one in the third parameter, if he wants replace the whole 'String' for a 'char' there's no need for a 'method', only an assignment will work i.e.: String a='a'+"" :)
Correct, but what if he wants to do this frequently? He will want to follow good programming practice and create a method to clean up his code.
Whith the second method he can do exactly the same setting as the second parameter the original String and a String with lenght of 1 i.e.: String a="aes";a=replace(a, a, "e");, the result will be a String with a value of e, and with this he will not lose the original functionality of the method
|
0

the simplest way:

"value_".replace("_",String.valueOf((char)58 )); //return "value:"

EDIT:

(char)58 //return: ':'
(int)':' //return: 58

Since we want to work with codes and no character we have to pass code to character

another problem to solve is that method "replace" does not take "(String x,char y)"

So we pass our character to String this way:

String.valueOf((char)58) //this is a String like this ":"

Finally we have String from int code, to be replaced.

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
0

"String is a sequence of characters"

    String s="hello";
    char c='Y';
    System.out.println(s.replace(s, Character.toString(c)));

    Output:
    Y

1 Comment

Guys and girls... s.replace(s, foo) is a complicated way of writing foo. Using the String replace method makes sense when used with substrings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.