我喜歡三個詞的表達:「關上門」,我想在一個句子中找到它。由於他們是由太空分離什麼是最好的解決方案。在一個句子中找到一個字符串表達式
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. 在一個字符串中查找多個正則表達式
- 3. js正則表達式在字符串中找到多個子字符串
- 4. 找到一個子字符串中的
- 5. 正則表達式來找到一個詞在句子
- 6. C#正則表達式找到組缺少一個字符串?
- 7. 找到一個正則表達式以下字符串
- 8. 找到一個匹配字符串的正則表達式?
- 9. 正則表達式找到一個字符串
- 10. 在另一個字符串中查找JSON字符串的正則表達式
- 11. 一個字符串正則表達式
- 12. 正則表達式,得到第一個符號「&」在字符串
- 13. 找到一個子串和插入另一個字符串
- 14. 正則表達式來找到一個字符串中的單個點
- 15. 正則表達式在字符串中找到一些東西
- 16. XPath表達式,兩個字符串在一個節點中
- 17. 在C中找到一個字符串
- 18. 如何在兩個子串之間找到一個字符串?
- 19. 正則表達式在PHP中:找到第一個匹配的字符串
- 20. 使用正則表達式提取一個句子的最後一個字符
- 21. 正則表達式從一個句子
- 22. 在正則表達式模式中添加一個字符串
- 23. 找到一個字符串,但返回前一個字符串
- 24. 模式只找到一個正則表達式的字符
- 25. 在一個字符串替換字符與正則表達式
- 26. 從一個lambda表達式創建一個字符串
- 27. 正則表達式:匹配,直到找到第一個]字符
- 28. 在字符串中找到一個數組中的字符串
- 29. 使用正則表達式去除一個子字符串
- 30. 如何拆分一個字符串並在另一個字符串中找到一個字符串?
正則表達式有一個很大的開銷,這我不知道,需要在這種情況下。 – 2011-03-17 15:48:06