正则表达式从字符串中删除\t \r \ n [英] Regex to remove \t \r \n from string

查看:1980
本文介绍了正则表达式从字符串中删除\t \r \ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的字符串中删除一些字符(\t

\ r \ n,这些字符串在整个字符串中),我知道正则表达式可以做到这一点,但我

不知道怎么做。任何指针都非常赞赏。


克里斯

Hi, i would like to remove a number of characters from my string (\t
\r \n which are throughout the string), i know regex can do this but i
have no idea how. Any pointers much appreciated.

Chris

推荐答案

Chris,


为什么不在String类上使用三次调用Replace方法呢?

string myString = input.Replace(" \t"," ")。替换(" \r","")。替换(" \ n",

"");


如果您愿意,也可以在这里使用角色版本。


-

- Nicholas Paldino [.NET / C#MVP] < br $> b $ b - mv*@spam.guard.caspershouse.com


< mo ***** @ gmail.com写信息

新闻:11 ********************** @ y80g2000hsf.googlegr oups.com ...
Chris,

Why not just use three calls to the Replace method on the String class?

string myString = input.Replace("\t", "").Replace("\r", "").Replace("\n",
"");

You can use the character version here as well if you wish.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<mo*****@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...

您好,我想从我的字符串中删除一些字符(\t

\ r \ n这是整个字符串),我知道正则表达式可以做到这一点,但我

hav不知道怎么做。任何指针都非常赞赏。


Chris
Hi, i would like to remove a number of characters from my string (\t
\r \n which are throughout the string), i know regex can do this but i
have no idea how. Any pointers much appreciated.

Chris


为什么不在String上使用三次调用Replace方法class?


我目前正在使用3个替换调用:),但是我总是

避免使用reglular表达式之前这似乎是理想的借口

学习它们!我也有兴趣将字符串中的\\ n转换为

也只是\ n。我肯定一定有可能吗?


Why not just use three calls to the Replace method on the String class?

I am currently using the 3 replace calls :), however i have always
avoided reglular expressions before this seemed the ideal excuse to
learn them! I would also be interested in turning \r\n in a string to
just \n also. im sure it must be possible?



当然,只是想知道为什么你不会采取更简单,更多的

可维护(取决于谁在看它,至少从我的观点来看)b $ b视图)方法。 =)


在这种情况下,我相信你可以有一个正则表达式[\\\\\ n] \\ n>

然后调用Replace方法,传递你的输入字符串和一个空的

with)它应该工作。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< mo ***** @ gmail.comwrote in message

新闻:11 ********************** @ y80g2000hsf.googlegr oups.com ...
Absolutely, just wondering why you wouldn''t take the simpler, more
maintainable (depending on who is looking at it, at least from my point of
view) approach. =)

In this case, I believe you can have a regular expression of "[\t\r\n]"
and then call the Replace method, passing your input string and an empty
string (or whatever you want to replace any of the characters in that set
with) and it should work.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<mo*****@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...

>为什么不在String
类上使用三次调用Replace方法?
> Why not just use three calls to the Replace method on the String
class?



我目前正在使用3个替换调用:),但是我总是在这之前避免使用
似乎是理想的借口

学习它们!我也有兴趣将字符串中的\\ n转换为

也只是\ n。我肯定一定可以吗?


I am currently using the 3 replace calls :), however i have always
avoided reglular expressions before this seemed the ideal excuse to
learn them! I would also be interested in turning \r\n in a string to
just \n also. im sure it must be possible?


这篇关于正则表达式从字符串中删除\t \r \ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆