C# 在句子中查找字符串表达式
我有一个三个字的表达:“关上门”,我想在一个句子中找到它。因为它们是按空间分开的,所以最好的解决方案是什么。如果您有字符串:C# 在句子中查找字符串表达式,c#,.net,vb.net,C#,.net,Vb.net,我有一个三个字的表达:“关上门”,我想在一个句子中找到它。因为它们是按空间分开的,所以最好的解决方案是什么。如果您有字符串: if (string1.indexOf(string2) >= 0) ... string sample = "If you know what's good for you, you'll shut the door!"; 如果你想找到它在句子中的位置,你可以使用IndexOf方法 int index = sample.IndexOf("shut the
if (string1.indexOf(string2) >= 0)
...
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表示它不存在于字符串中。请注意,搜索字符串(“关上门”)区分大小写 如果您有字符串:
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表示它不存在于字符串中。请注意,搜索字符串(“关上门”)区分大小写 空格没有什么特别的,它们只是字符,所以你可以找到这样的字符串,就像yuo会在你的句子中找到任何其他字符串一样,例如,如果你需要位置,可以使用“indexOf”,如果你需要知道它是否存在,可以使用“Contains” 例如
string sentence = "foo bar baz";
string phrase = "bar baz";
Console.WriteLine(sentence.Contains(phrase)); // True
空格没有什么特别的,它们只是字符,所以你可以找到这样的字符串,就像yuo会在你的句子中找到任何其他字符串一样,例如,如果你需要位置,使用“indexOf”,或者如果你需要知道它是否存在,只使用“Contains” 例如
string sentence = "foo bar baz";
string phrase = "bar baz";
Console.WriteLine(sentence.Contains(phrase)); // True
使用内置Regex.Match方法匹配字符串
string text = "One car red car blue car";
string pat = @"(\w+)\s+(car)";
// Compile the regular expression.
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
// Match the regular expression pattern against a text string.
Match m = r.Match(text);
int matchCount = 0;
while (m.Success)
{
Console.WriteLine("Match"+ (++matchCount));
for (int i = 1; i <= 2; i++)
{
Group g = m.Groups[i];
Console.WriteLine("Group"+i+"='" + g + "'");
CaptureCollection cc = g.Captures;
for (int j = 0; j < cc.Count; j++)
{
Capture c = cc[j];
System.Console.WriteLine("Capture"+j+"='" + c + "', Position="+c.Index);
}
}
m = m.NextMatch();
}
string text=“一辆车红色车蓝色车”;
字符串pat=@“(\w+)\s+(car)”;
//编译正则表达式。
Regex r=新的Regex(pat,RegexOptions.IgnoreCase);
//将正则表达式模式与文本字符串匹配。
匹配m=r.Match(文本);
int matchCount=0;
while(m.Success)
{
Console.WriteLine(“匹配”+(++matchCount));
对于(inti=1;i使用内置Regex.Match方法来匹配字符串
string text = "One car red car blue car";
string pat = @"(\w+)\s+(car)";
// Compile the regular expression.
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
// Match the regular expression pattern against a text string.
Match m = r.Match(text);
int matchCount = 0;
while (m.Success)
{
Console.WriteLine("Match"+ (++matchCount));
for (int i = 1; i <= 2; i++)
{
Group g = m.Groups[i];
Console.WriteLine("Group"+i+"='" + g + "'");
CaptureCollection cc = g.Captures;
for (int j = 0; j < cc.Count; j++)
{
Capture c = cc[j];
System.Console.WriteLine("Capture"+j+"='" + c + "', Position="+c.Index);
}
}
m = m.NextMatch();
}
string text=“一辆车红色车蓝色车”;
字符串pat=@“(\w+)\s+(car)”;
//编译正则表达式。
Regex r=新的Regex(pat,RegexOptions.IgnoreCase);
//将正则表达式模式与文本字符串匹配。
匹配m=r.Match(文本);
int matchCount=0;
while(m.Success)
{
Console.WriteLine(“匹配”+(++matchCount));
对于(int i=1;i这里有一些C#代码,使用开始字符串和结束字符串点查找子字符串,但您可以将其用作基础并修改(即删除对结束字符串的需要)以仅查找您的字符串。。。
两个版本,一个只查找子字符串的第一个实例,另一个返回子字符串和实际字符串的所有起始位置的字典
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;
}
public Dictionary GetSubstringDic(字符串开始、字符串结束、字符串源、bool includeStartEnd、bool不区分大小写)
{
int startIndex=-1;
int-endIndex=-1;
整数长度=-1;
int sourceLength=source.Length;
字典结果=新字典();
尝试
{
//如果只想查找字符串,则不区分大小写
if(不区分大小写)
{
source=source.ToLower();
start=start.ToLower();
end=end.ToLower();
}
//起始字符串存在吗
startIndex=source.IndexOf(开始);
如果(startIndex!=-1)
{
//开始检查每个匹配实例的源字符串长度
while(startIndex-1)
{
//是否存在结束字符串?
endIndex=source.IndexOf(end,startIndex+1);
如果(endIndex!=-1)
{
//如果我们想得到字符串的长度,包括开始字符串和结束字符串
如果(包括启动)
{
//确保包含结束字符串
长度=(endIndex+end.length)-startIndex;
}
其他的
{
//将开始索引更改为不包含开始字符串
startIndex=startIndex+start.Length;
长度=endIndex-startIndex;
}
//添加到字典
Add(startIndex,source.Substring(startIndex,length));
//向上移动起始位置
startIndex=source.IndexOf(开始,结束索引+1);
}
其他的
{
//没有尽头,就这样冲出去了;
打破
}
}
}
}
捕获(例外情况除外)
{
//通知错误
结果=新字典();
StringBuilder g_错误=新建StringBuilder();
g_Error.AppendLine(“GetSubstringDic:+ex.Message.ToString());
g_Error.AppendLine(例如StackTrace.ToString());
}
返回结果;
}
公共字符串GetSubstring(字符串开始、字符串结束、字符串源、bool includeStartEnd、bool不区分大小写)
{
int startIndex=-1;
int-endIndex=-1;
整数长度=-1;
int sourceLength=source.Length;
字符串结果=string.Empty;
尝试
{
if(不区分大小写)
{
source=source.ToLower();
start=start.ToLower();
end=end.ToLower();
}
startIndex=source.IndexOf(开始);
如果(startIndex!=-1)
{
endIndex=source.IndexOf(end,startIndex+1);
如果(endIndex!=-1)
{
如果(包括启动)
{
长度=(endIndex+end.length)-startIndex;
}
其他的
{
startIndex=startIndex+start.Length;
长度=endIndex-startIndex;
}
结果=源.子字符串(startIndex,长度);
}
}
}
捕获(例外情况除外)
{
//通知错误
result=string.Empty;
StringBuilder g_错误=新建StringBuilder();
g_E