-2

Possible Duplicate:
How do I replace a character in a string in Java?

how can i replace all occurences of '\x0d' in a Java String??

  • I have tried myString.replaceAll("\x0d", "")... does not works

and all the answers below does not work have tried that already

OMG :)


Apologies for the crap guys.

Community
  • 1
  • 1
anonymous
  • 2,009
  • 3
  • 16
  • 13
  • 3
    By "\x0d" do you mean the character with ASCII code 13 or the string consisting of the four characters '\', 'x', '0' and 'd'? – Mark Byers Sep 27 '11 at 00:31
  • 3
    (Honestly, you should be ashamed of yourself for not going to the online reference for String and scanning it for any function that might do the job. This is basic stuff that any first-week programming student should understand how to do.) – Hot Licks Sep 27 '11 at 00:32
  • You can’t: Java strings are immutable. :) – tchrist Sep 27 '11 at 00:50
  • Can you show the full line of code that you have used for the replaceAll? – Chad Sep 27 '11 at 01:04
  • @fireshadow52 I am just looking for an answer ! – anonymous Sep 27 '11 at 01:04
  • @tchrist, I am not trying to change the existing string.. replace and put it in a new string... there must be a way ! – anonymous Sep 27 '11 at 01:06
  • 1
    @anonymous: Your question is poorly asked. Had you mentioned what you had already tried, you'd be getting much better answers. Don't be lazy crafting a good question... – Ruan Mendes Sep 27 '11 at 01:06
  • @anonymous Six people, at least, have given you the damn answer! What more do you want, especially since it's something you could have easily looked up yourself??? – Hot Licks Sep 27 '11 at 01:07
  • @anonymous -- Are you trying to replace the single character '/r' (carriage return) or the character sequence "/0x0d"?? You coded the value in single quotes above, suggesting you think it's a single character, though, of course `\x0d` is not a valid Java escape sequence. – Hot Licks Sep 27 '11 at 01:31
  • @Daniel R Hicks Oh Daniel, you poor boy, you do not understand that NONE OF THESE SOLUTIONS WORK ! I have tried them all ! – anonymous Sep 27 '11 at 04:00
  • @anonymous: I asked you a question. You didn't answer it. If you helped us to help you then we would be able to help you better. Please post a complete example of what you are trying to do, including an example input string. – Mark Byers Sep 27 '11 at 05:06
  • @anonymous -- Interesting -- I tried several and they worked. Either you're a troll or you're blazingly incompetent and ought to become a street mime or such. – Hot Licks Sep 27 '11 at 11:35
  • @Mark Byers I have edited the question Mark you can now see an example up there. Thanks for your help ! – anonymous Sep 27 '11 at 12:46
  • @anonymous: I still can't see any example input strings, and I still can't see any explanation of whether you are trying to replace the character with ASCII value 13, or the string consisting of the characters "\", "x", "0", "d". – Mark Byers Sep 27 '11 at 12:51
  • @anonymous -- Did you try ASSIGNING the result value from replaceAll to a String variable and printing it?? Note that there is no function that will modify a String object, since String is immutable. – Hot Licks Sep 27 '11 at 17:27

5 Answers5

1

Escape the backslash:

myString = myString.replace("\\x0d", "whateverYouWantToReplaceWith")

See it working on ideone.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

I believe you want this:

String test = "\r".replace("\r", "princess");
System.out.println(test);  // princess

ASCII 13 is Carriage Return

Joe
  • 80,724
  • 18
  • 127
  • 145
0

You may consider trying String.replace.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

Just a guess -- String.replace? There's a character version and a "character sequence" version.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0
myString.replaceAll("\\x0d","whateverYouWantToReplaceWith");
fireshadow52
  • 6,298
  • 2
  • 30
  • 46
  • 1
    So you and Mark figure he intends to replace the CHARACTERS "\x0D"? Yep, in that case your examples are good ones. – Hot Licks Sep 27 '11 at 01:28
  • @Daniel R Hicks: Our answers are not the same. This uses replaceAll, which treats the \\x0d as regular expression, where it means the ASCII character with code 13. http://ideone.com/7XRp3 – Mark Byers Sep 27 '11 at 06:19