在C#中删除字符串中前导特殊字符的最快方法 [英] Fastest way to remove the leading special characters in string in c#

查看:74
本文介绍了在C#中删除字符串中前导特殊字符的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#,并且有一个类似

I am using c# and i have a string like

-Xyz
--Xyz
---Xyz
-Xyz-Abc
--Xyz-Abc

我只是想删除任何前导特殊字符,直到字母表出现为止,注意:字符串中间的特殊字符将保持不变.最快的方法是什么?

i simply want to remove any leading special character until alphabet comes , Note: Special characters in the middle of string will remain same . What is the fastest way to do this?

推荐答案

您可以使用 string.TrimStart 并传入您要删除的字符:

You could use string.TrimStart and pass in the characters you want to remove:

var result = yourString.TrimStart('-', '_');

但是,这仅是一个好主意,如果您要删除的特殊字符的数目众所周知且很小.
如果不是这种情况,则可以使用正则表达式:

However, this is only a good idea if the number of special characters you want to remove is well-known and small.
If that's not the case, you can use regular expressions:

var result = Regex.Replace(yourString, "^[^A-Za-z0-9]*", "");

这篇关于在C#中删除字符串中前导特殊字符的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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