【发布时间】:2016-10-07 09:55:28
【问题描述】:
从一个字符串中,检查它是否以值“startsWithCorrectId”开头...如果它确实从一开始就删除了该值。问题是如果在字符串中再次找到此值,它也会将其删除。我意识到这就是 .replace 所做的......但是有没有类似 .startsWith 到 RemoveAtStart 的东西?
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' 我只想替换第一个实例
【问题讨论】:
-
可以使用
IndexOf和Substring,也可以使用Regex.Replace -
只有在开头,还是第一次出现?
-
对您收到的 4 个答案有任何反馈...?
-
这方面有什么更新吗?
标签: c#