c# - 在c#中转到子字符串的末尾
问题描述
注释 // 到结尾,我不知道如何干净地结束子字符串 :( 有没有更简单的方法可以走到子字符串的末尾而不是自己算出数字?对于更复杂的字符串,这将太难了
string word = Console.ReadLine();
string[] lines = File.ReadAllLines(file);
using (var far = File.CreateText(resultfile))
{
foreach (string line in lines)
{
StringBuilder NewL = new StringBuilder();
int ind = line.IndexOf(word);
if (ind >= 0)
{
if (ind == 0)
{
NewL.Append(line.Substring(ind+ word.Length +1, // go to end);
}else{
NewL.Append(line.Substring(0, ind - 1));
NewL.Append(line.Substring(ind + word.Length + 1, // go to end));}
far.WriteLine(NewL);
}
else
{
far.WriteLine(line);
}
}
我不知道stackoverflow想要什么更多的细节,任何能回答这个问题的人都可以清楚地理解这个简单的代码。
解决方案
您可以使用String.Substring(int)
重载,它会自动继续到源字符串的末尾:
NewL.Append(line.Substring(ind + word.Length + 1));
从此实例中检索子字符串。子字符串从指定的字符位置开始,一直持续到字符串的末尾。
推荐阅读
- unit-testing - “无效使用参数匹配器”,但我只使用匹配器
- javascript - 带有javascript的组合框下拉树?
- reactjs - 胜利 | 如何从最低的负 y 轴值显示 x 轴
- javascript - onClick 使用 jquery 显示当前 div
- javascript - 无法在 PWA (Firebase) 中获取自定义推送通知事件
- crystal-reports - 在运行时将 Crystal Reports 数据源从 MSACCESS 更改为 SQLSERVER
- java - 我在 firebase library 中找到了重复的类。如何排除?
- oracle - PL/SQL:ORACLE 中的感叹号
- android - 如何在android的workmanager中使用自定义约束
- apache-kafka - 在 Kafka 连接器中,如何获取我的 Kafka 连接器当前使用的引导服务器地址?