0

I have problem of replacing "…" in java. This is driving me crazy.

It seems that java can't recognize "…" by replace method. What should I try?

I tried

    line = line.replace("…", "");

But it doesn't work.

user1167910
  • 145
  • 2
  • 12
  • 3
    Are you trying to replace three dot characters, or the single character of the ellipsis? (U+2026) – Jon Skeet Dec 05 '13 at 18:22
  • @JonSkeet the single character – user1167910 Dec 05 '13 at 18:22
  • 2
    Could you post example code that we could use to reproduce your problem because it seems to be working as it should http://ideone.com/E0eNNi. – Pshemo Dec 05 '13 at 18:25
  • What happens if you try `line = line.replace("\u2026","");`? – ajb Dec 05 '13 at 18:27
  • @Pshemo Which message would have priority over which other message? This could get very complicated for little gain … Take this as an input and replace the ... at the end. The "..." should be a single character – user1167910 Dec 05 '13 at 18:30
  • @SotiriosDelimanolis I am very sure that I didn't use three dots it is a single character. – user1167910 Dec 05 '13 at 18:32
  • `Which message would have priority over which other message? This could get very complicated for little gain …` seems to be working as it should http://ideone.com/d27dcG – Pshemo Dec 05 '13 at 18:35

3 Answers3

0

Try this redirect: Eclipse character encoding

This isn't exactly related, but it is a setting in eclipse that you should check nonetheless.

Community
  • 1
  • 1
user6993
  • 41
  • 3
0

You are likely having a Unicode translation issue. Try using "\u2026" instead of the literal ellipsis character: http://www.fileformat.info/info/unicode/char/2026/index.htm and Creating Unicode character from its number. You can not always control how unicode characters will be interpreted in your source code beyond the ASCII set. It is generally a good idea to use escapes for such characters.

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0

The String.replaced only char in the older javas, but nowadays also can replace strings (literally without regex patterns).

The problem you might have encountered, is that of wrong encoding. The encoding of the .java source in the editor versus the compiler's encoding. If you pasted the ellipsis, something might go wrong too.

So use the escaped ASCII representation. You can find the code in the system accessory Character Map:

line = line.replace("\u2026", "");

Also the line must have been read with the correct encoding. Do not use FileReader, but use another class where one can add explicitly the encoding.

BufferedReader in = new BufferedReader(new InputStreamReader(
        new FileInputStream(file), "UTF-8"));

Instead of the encoding UTF-8 it could be some platform dependent code like Cp1252.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • What do FileReader and BufferedReader have anything to do with the question. The point about encoding is valid though. – Mad Physicist Dec 05 '13 at 18:39
  • @MadPhysicist if the line contained a wrongly encoded ellipsis, it could go wrong too. – Joop Eggen Dec 05 '13 at 18:41
  • 1
    Agreed. As a general safety percaution, you could not be more right. I am just saying that the question is not talking about reading anything from a file. – Mad Physicist Dec 05 '13 at 18:42