-1

I have a problem in c# with replace in a string with space at the end.

"ERROR.1️.1.1094".Replace("ERROR.1.", "")

Expected value is 1.1094 but it's not working.

Can anybody help me?

Gabriel
  • 17
  • 2
  • 3
    `Replace` returns a new string, are you not using that value? – DavidG Jan 28 '22 at 09:59
  • Console.WriteLine("ERROR 1️ 1.1094".Replace("ERROR 1 ", "")); doesn't write 1.1094? – Cetin Basoz Jan 28 '22 at 10:01
  • 2
    When a method isn't working, please refer to the documentation as the first port of call: https://learn.microsoft.com/en-us/dotnet/api/system.string.replace?view=net-6.0 - The documentation states: _"**Returns a new string** in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String."_ – ProgrammingLlama Jan 28 '22 at 10:02
  • Actually, even [Intellisense tells you the same thing](https://i.stack.imgur.com/m0mQl.png). – ProgrammingLlama Jan 28 '22 at 10:04
  • It's actually a typo. `"ERROR 1️" != "ERROR 1"` both have a single character different. `"1️" != "1"` (Try it in the JS console). – Cid Jan 28 '22 at 10:09
  • The duplicate is incorrect, but the question needs to be remained closed (Typo) – Cid Jan 28 '22 at 10:12
  • The error is also in this example: >"ERROR.1️..1.1094".Replace("ERROR.1.", "") – Gabriel Jan 28 '22 at 10:12
  • Gabriel: "but it's not working" is not an adequate problem description. How are we supposed to understand what you expect "working" to be? How do we know what you believe is evidence that it's "not working"? Please edit your question with a [mcve]. – ProgrammingLlama Jan 28 '22 at 10:14
  • The problem comes from the character used for the digit `one` in the string `"ERROR.1️.1.1094"` It's actually the unicode character [Segmented Digit One](https://www.compart.com/en/unicode/U+1FBF1) and **not** the well known *"Digit one"* (ASCII 0x31) which is used in the second part, the string to be searched *"ERROR.1."* – Cid Jan 28 '22 at 10:26
  • @Cid That's not the problem. The error persists testing with the string like this: "ERRORC1️C1.1094".Replace("ERRORC1C", "") – Gabriel Jan 28 '22 at 10:31
  • 1
    @Gabriel that's the same thing, the caracter you're using for `1` in `"ERRORC1️C1.1094"` is **different** than the character used in `"ERRORC1C"`. That's like trying to compare `'a'` and `'B'` – Cid Jan 28 '22 at 10:32
  • @Cid Okay I understand now. Is there any alternative to use? – Gabriel Jan 28 '22 at 10:36
  • Well, use the same character from both sides – Cid Jan 28 '22 at 10:39
  • @Gabriel [check this](https://dotnetfiddle.net/SxeoVV) – Cid Jan 28 '22 at 10:42
  • Thanks @Cid. God bless you – Gabriel Jan 28 '22 at 12:24
  • @Gabriel God doesn't exist, but thanks anyway – Cid Jan 28 '22 at 16:19

1 Answers1

-2

Please try the following
"ERROR 1️ 1.1094".Replace("ERROR 1 ", string.Empty)

GSM
  • 87
  • 11