我喜欢三个词的表达:“关上门”,我想在一个句子中找到它。由于他们是由太空分离什么是最好的解决方案。在一个句子中找到一个字符串表达式
1
A
回答
4
如果你有字符串:
string sample = "If you know what's good for you, you'll shut the door!";
而且要找到它是在一个句子,你可以使用indexOf方法。
int index = sample.IndexOf("shut the door");
// index will be 42
非-1答案表示该字符串已定位。 -1表示它不存在于字符串中。请注意,搜索字符串(“关上门”)区分大小写。
0
if (string1.indexOf(string2) >= 0)
...
0
空格没什么特别的,它们只是字符,所以你可以找到像这样的字符串,就像你会找到任何其他字符串在你的句子中,例如,如果你需要的位置使用“indexOf”,或者只是“Contains “如果你需要知道它是否存在。
E.g.
string sentence = "foo bar baz";
string phrase = "bar baz";
Console.WriteLine(sentence.Contains(phrase)); // True
2
0
下面是一些C#代码使用开始串和结束串点,以找到一个子,但你可以作为一个基础使用和修改(即删除不需要端线)只是找到你的字符串... 2个版本,一个只是找到一个子字符串的第一个实例,另一个返回一个子字符串的所有起始位置和实际字符串的字典。
public Dictionary<int, string> GetSubstringDic(string start, string end, string source, bool includeStartEnd, bool caseInsensitive)
{
int startIndex = -1;
int endIndex = -1;
int length = -1;
int sourceLength = source.Length;
Dictionary<int, string> result = new Dictionary<int, string>();
try
{
//if just want to find string, case insensitive
if (caseInsensitive)
{
source = source.ToLower();
start = start.ToLower();
end = end.ToLower();
}
//does start string exist
startIndex = source.IndexOf(start);
if (startIndex != -1)
{
//start to check for each instance of matches for the length of the source string
while (startIndex < sourceLength && startIndex > -1)
{
//does end string exist?
endIndex = source.IndexOf(end, startIndex + 1);
if (endIndex != -1)
{
//if we want to get length of string including the start and end strings
if (includeStartEnd)
{
//make sure to include the end string
length = (endIndex + end.Length) - startIndex;
}
else
{
//change start index to not include the start string
startIndex = startIndex + start.Length;
length = endIndex - startIndex;
}
//add to dictionary
result.Add(startIndex, source.Substring(startIndex, length));
//move start position up
startIndex = source.IndexOf(start, endIndex + 1);
}
else
{
//no end so break out of while;
break;
}
}
}
}
catch (Exception ex)
{
//Notify of Error
result = new Dictionary<int, string>();
StringBuilder g_Error = new StringBuilder();
g_Error.AppendLine("GetSubstringDic: " + ex.Message.ToString());
g_Error.AppendLine(ex.StackTrace.ToString());
}
return result;
}
public string GetSubstring(string start, string end, string source, bool includeStartEnd, bool caseInsensitive)
{
int startIndex = -1;
int endIndex = -1;
int length = -1;
int sourceLength = source.Length;
string result = string.Empty;
try
{
if (caseInsensitive)
{
source = source.ToLower();
start = start.ToLower();
end = end.ToLower();
}
startIndex = source.IndexOf(start);
if (startIndex != -1)
{
endIndex = source.IndexOf(end, startIndex + 1);
if (endIndex != -1)
{
if (includeStartEnd)
{
length = (endIndex + end.Length) - startIndex;
}
else
{
startIndex = startIndex + start.Length;
length = endIndex - startIndex;
}
result = source.Substring(startIndex, length);
}
}
}
catch (Exception ex)
{
//Notify of Error
result = string.Empty;
StringBuilder g_Error = new StringBuilder();
g_Error.AppendLine("GetSubstring: " + ex.Message.ToString());
g_Error.AppendLine(ex.StackTrace.ToString());
}
return result;
}
0
您可能想要确保检查忽略两个短语的情况。
string theSentence = "I really want you to shut the door.";
string thePhrase = "Shut The Door";
bool phraseIsPresent = theSentence.ToUpper().Contains(thePhrase.ToUpper());
int phraseStartsAt = theSentence.IndexOf(
thePhrase,
StringComparison.InvariantCultureIgnoreCase);
Console.WriteLine("Is the phrase present? " + phraseIsPresent);
Console.WriteLine("The phrase starts at character: " + phraseStartsAt);
此输出:
Is the phrase present? True
The phrase starts at character: 21
相关问题
- 1. 用java的正则表达式找到一个子字符串
- 2. 找到一个子字符串并匹配一个大字符串在PHP中的请求表达式?
- 3. 在一个字符串中查找多个正则表达式
- 4. 用正则表达式在句子中找到一个词
- 5. 正则表达式找到2号在一个字符串
- 6. js正则表达式在字符串中找到多个子字符串
- 7. 找到一个子字符串中的
- 8. 正则表达式来找到一个词在句子
- 9. 找到一个匹配字符串的正则表达式?
- 10. 找到一个正则表达式以下字符串
- 11. 正则表达式找到一个字符串
- 12. C#正则表达式找到组缺少一个字符串?
- 13. 在另一个字符串中查找JSON字符串的正则表达式
- 14. 如何在Java中使用正则表达式从一个字符串中查找多个子字符串?
- 15. 一个字符串正则表达式
- 16. 在字符串中查找两个第一个字(正则表达式)
- 17. 正则表达式,得到第一个符号“&”在字符串
- 18. 正则表达式找到一个子字符串开头,以特定的词
- 19. 找到一个子串和插入另一个字符串
- 20. 正则表达式来找到一个字符串中的单个点
- 21. 正则表达式在一个字符串中的多个字符串
- 22. XPath表达式,两个字符串在一个节点中
- 23. 找到句子的简单方法是一个大字符串?
- 24. 在C中找到一个字符串
- 25. 如何在两个子串之间找到一个字符串?
- 26. 正则表达式在PHP中:找到第一个匹配的字符串
- 27. 使用正则表达式提取一个句子的最后一个字符
- 28. 正则表达式从一个句子
- 29. 查找在另一个字符串列表中有一个项目的子字符串的所有字符串
- 30. 在正则表达式模式中添加一个字符串
正则表达式有一个很大的开销,这我不知道,需要在这种情况下。 – 2011-03-17 15:48:06