c# - 从字符串中修剪字符串
问题描述
因此,从字符串中修剪某些内容的正常方法是按字符修剪它。
例如:
string test = "Random Text";
string trimmedString = test.Trim(new Char[] { 'R', 'a', 'n'});
Console.WriteLine(trimmedString);
//the output would be "dom Text"
但不是这样做,有没有办法从字符串中完全删除组合字符“Ran”?
例如:
string test = "Random Text";
string trimmedString = test.Trim(string "Ran");
Console.WriteLine(trimmedString);
//the output would be "dom Text"
现在,上面的代码给出了一个错误,但想知道这样的事情是否可能,谢谢!
解决方案
您可以像这样使用删除:
string test = "Random Text";
string textToTrim = "Ran";
if (test.StartsWith(textToTrim))
test = test.Remove(0, textToTrim.Length);
if (test.EndsWith(textToTrim))
test = test.Remove(test.Length - textToTrim.Length);
推荐阅读
- azure-logic-apps - 如何在逻辑应用程序中获取 HTTP 发布操作的状态码?
- windows - 服务没有响应控制功能
- php - 使用数组元素作为对象成员变量在 php 7 中不起作用
- .net - 如何在 Nginx 服务器中将 ASP.NET Core Web API dll 文件作为服务运行?
- jquery - 更新更改行jquery的数据顺序后动态附加的数据表
- php - Wordpress sql插入表单
- python - 使用 Python Play-Scraper 模块 - 无法使用模块检索数据
- android - 安卓绿色强调色
- autodesk-forge - 设计自动化,处理后工作项输出改变
- python - 将通过 UDP 发送的方法更改为通过 TCP 发送