简体   繁体   English

C#替换字符串中的特殊字符

[英]C# replacing a special character in a string

This used to be easy in VB but I'm completely stumped in c# VS2010. 这曾经很容易在VB中,但我完全被c#VS2010难倒。

I am receiving data form a serial port. 我从串口接收数据。 At the end of each line there is a special character which indicates EndOfText. 在每行的末尾有一个特殊字符,表示EndOfText。 If you look at it in the terminal window it looks like a little L-shaped bracket. 如果你在终端窗口看它,它看起来像一个小L形支架。 That character is the ASCII "03" 那个字符是ASCII“03”

I need to replace that character with a crlf ("\\r\\n"). 我需要用crlf(“\\ r \\ n”)替换该字符。

I have tried the following and it's throwing "Invalid Argument" errors: 我尝试了以下内容,并抛出“无效参数”错误:

 RxString = RxString.Replace((char) 03, "\r\n");

I can trim the end character and then perform and append of the CRLF "\\r\\n" but would rather do it properly in one pass. 我可以修剪结束字符,然后执行并附加CRLF“\\ r \\ n”,但宁愿在一次通过中正确执行。

There's no overload of string.Replace taking a char then a string . 有没有过载string.Replace采取char ,然后一个string This should work though: 这应该工作:

RxString = RxString.Replace("\u0003", "\r\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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