C# 仅删除字符串中的第一个匹配项
从字符串中,检查它是否以值“startsWithCorrectId”开头……如果它确实从开头删除了该值。问题是,如果在字符串中再次找到此值,它也会将其删除。我意识到这就是为什么。replace是…但是是否有类似于。startsWith的东西来删除启动C# 仅删除字符串中的第一个匹配项,c#,C#,从字符串中,检查它是否以值“startsWithCorrectId”开头……如果它确实从开头删除了该值。问题是,如果在字符串中再次找到此值,它也会将其删除。我意识到这就是为什么。replace是…但是是否有类似于。startsWith的东西来删除启动 string startsWithCorrectId = largeIconID.ToString(); //startsWithCorrectId will be '1' string fullImageName = file.Replace(
string startsWithCorrectId = largeIconID.ToString();
//startsWithCorrectId will be '1'
string fullImageName = file.Replace("_thumb", "");
//fullImageName will be "1red-number-1.jpg"
//file will be '1red-number-1_thumb.jpg'
if(file.StartsWith(startsWithCorrectId))
{
fullImageName = fullImageName.Replace(startsWithCorrectId, "");
//so yes this is true but instead of replacing the first instance of '1'..it removes them both
}
实际上,我希望“1red-number-1.jpg”变成“red-number-1.jpg”…而不是“red-number-.jpg”…替换“startsWithCorrectId”的所有实例我只想替换第一个实例一个解决方案是使用:
这将删除startsWithCorrectId
,如果它位于字符串的开头一种解决方案是使用:
这将删除
startsWithCorrectId
如果它位于字符串的开头如果我没有正确理解您的字符串,您需要从correctId.Length位置获取一个字符串
if(fullImageName .StartsWith(startsWithCorrectId))
fullImageName = fullImageName .Substring(startsWithCorrectId.Length);
如果您喜欢扩展:
public static class StringExtensions{
public static string RemoveFirstOccuranceIfMatches(this string content, string firstOccuranceValue){
if(content.StartsWith(firstOccuranceValue))
return content.Substring(firstOccuranceValue.Length);
return content;
}
}
//...
fullImageName = fullImageName.RemoveFirstOccuranceIfMatches(startsWithCorrectId);
如果我没有正确理解你的代码,你需要得到一个从correctId.Length位置开始的字符串
if(fullImageName .StartsWith(startsWithCorrectId))
fullImageName = fullImageName .Substring(startsWithCorrectId.Length);
如果您喜欢扩展:
public static class StringExtensions{
public static string RemoveFirstOccuranceIfMatches(this string content, string firstOccuranceValue){
if(content.StartsWith(firstOccuranceValue))
return content.Substring(firstOccuranceValue.Length);
return content;
}
}
//...
fullImageName = fullImageName.RemoveFirstOccuranceIfMatches(startsWithCorrectId);
可以使用正则表达式执行此操作,其中可以对字符串从开头开始的要求进行编码:
var regex = "^" + Regex.Escape(startsWithCorrectId);
// Replace the ID at the start. If it doesn't exist, nothing will change in the string.
fullImageName = Regex.Replace(fullImageName, regex, "");
另一种选择是使用子字符串,而不是替换操作。您已经知道它位于字符串的开头,您可以将子字符串紧跟其后:
fullImageName = fullImageName.Substring(startsWithCorrectId.Length);
可以使用正则表达式执行此操作,其中可以对字符串从开头开始的要求进行编码:
var regex = "^" + Regex.Escape(startsWithCorrectId);
// Replace the ID at the start. If it doesn't exist, nothing will change in the string.
fullImageName = Regex.Replace(fullImageName, regex, "");
另一种选择是使用子字符串,而不是替换操作。您已经知道它位于字符串的开头,您可以将子字符串紧跟其后:
fullImageName = fullImageName.Substring(startsWithCorrectId.Length);
可复制的:您可以使用
IndexOf
和子字符串
,或使用Regex。仅当它在开始时,或仅在第一次出现时才替换
?对您收到的4个答案的任何反馈…?对此的任何更新?可复制的:您可以使用IndexOf
和子字符串
,或者只在开始时使用Regex.Replace
,或者仅仅是第一次出现?您收到的4个答案的任何反馈…?这方面的任何更新?为了清晰起见,我会使用startsWithCorrectId.Length
。为了清晰起见,我会使用startsWithCorrectId.Length
。@Andrew我在他的示例中也没有看到correctId
变量,但我认为你在回答中使用了这一点@Andrew我在他的示例中也没有看到correctId
变量,但我认为您在回答中使用了该变量。:)这并不是真正被问到的问题,但在我的情况下,它就像一个符咒一样有效(在我的上下文中,第一次出现总是在第一个位置)。这并不是真正被问到的问题,但在我的情况下,它就像一个符咒一样有效(在我的上下文中,第一次出现总是在第一个位置)。