C# 在C中从字符串中选择子字符串#

C# 在C中从字符串中选择子字符串#,c#,string,substring,C#,String,Substring,我正在寻找一个字符串中包含的字符串 假设我有两个字符串: String1 = "1.1)The Element is" String2 = "1.1)The Element is:(-) for the sub" 如果我比较String1和String2,我可以得到“1.1)元素是“,这是可以的 int Length_Str1 = string1.Length; string2 = string2.Remove(Length_Str1); 但我还想得到非字母顺序的字符“:-”。我想继

我正在寻找一个字符串中包含的字符串

假设我有两个字符串:

String1 = "1.1)The Element is"

String2 = "1.1)The Element is:(-) for the sub"
如果我比较String1和String2,我可以得到“1.1)元素是“,这是可以的

 int Length_Str1 = string1.Length;

 string2 = string2.Remove(Length_Str1);
但我还想得到非字母顺序的字符“:-”。我想继续提取字符,直到找到一个空格字符。但我不知道我怎么能用C

这个怎么样:

string s = "1.1)The Element is:(-) for the sub";
s = s.Substring(0, s.IndexOf("(-) ") + "(-) ".Length);
这使得
1.1)元素为:(-)

这为子系统提供了
。根据您的评论:

 string String1 = "1.1)The Element is";

 string String2 = "1.1)The Element is:(-) for the sub";
 if(String2.Contains(String1))
 {
      string s = String2.Substring(String2.IndexOf(String1)+ String1.Length);
      s = s.Substring(s.IndexOf(" ")+1);  // +1 to leave space
 }
这个怎么样:

string s = "1.1)The Element is:(-) for the sub";
s = s.Substring(0, s.IndexOf("(-) ") + "(-) ".Length);
这使得
1.1)元素为:(-)

这为子系统提供了
。根据您的评论:

 string String1 = "1.1)The Element is";

 string String2 = "1.1)The Element is:(-) for the sub";
 if(String2.Contains(String1))
 {
      string s = String2.Substring(String2.IndexOf(String1)+ String1.Length);
      s = s.Substring(s.IndexOf(" ")+1);  // +1 to leave space
 }

像这样使用
Split
string.Join
怎么样

var str1 = "1.1)The Element is";
var str2 = "1.1)The Element is:(-) for the sub";

str2 = string.Join(" ",str2.Split().Take(str1.Count(x => x == ' ')+1));

像这样使用
Split
string.Join
怎么样

var str1 = "1.1)The Element is";
var str2 = "1.1)The Element is:(-) for the sub";

str2 = string.Join(" ",str2.Split().Take(str1.Count(x => x == ' ')+1));

您可以执行以下操作:

至少如果字符串始终使用相同的格式;)

stringresult=string.Join(“”),string1.Split(“”).Take(3.ToArray());

我希望这对您有所帮助

您可以执行以下操作:

至少如果字符串始终使用相同的格式;)

stringresult=string.Join(“”),string1.Split(“”).Take(3.ToArray());

我希望这有帮助

只要
Char.isleter
Char.IsWhiteSpace
返回
false

int index = String2.IndexOf(String1);
if(index >= 0)
{
    string result = String1;
    if (String1.Length < String2.Length)
    {
        string rest = String2.Substring(index + String1.Length);
        var chars = rest.TakeWhile(c => !Char.IsLetter(c) && !Char.IsWhiteSpace(c));
        result = result + string.Join("", chars);
    }
}
int index=String2.IndexOf(String1);
如果(索引>=0)
{
字符串结果=String1;
if(String1.Length!Char.isleter(c)和&!Char.IsWhiteSpace(c));
result=result+string.Join(“,chars);
}
}

注意,您必须使用System.Linq添加
位于文件顶部。

只要
Char.islitter
Char.IsWhiteSpace
返回
false
,就可以使用Char:

int index = String2.IndexOf(String1);
if(index >= 0)
{
    string result = String1;
    if (String1.Length < String2.Length)
    {
        string rest = String2.Substring(index + String1.Length);
        var chars = rest.TakeWhile(c => !Char.IsLetter(c) && !Char.IsWhiteSpace(c));
        result = result + string.Join("", chars);
    }
}
int index=String2.IndexOf(String1);
如果(索引>=0)
{
字符串结果=String1;
if(String1.Length!Char.isleter(c)和&!Char.IsWhiteSpace(c));
result=result+string.Join(“,chars);
}
}

注意,您必须使用System.Linq添加
位于文件顶部。

以下代码应找到string1长度后第一个空格的索引

int Length_Str1 = string1.Length;

string2 = string2.Remove(string2.IndexOf(' ', Length_Str1));

请注意,您还应该添加一些检查,以确保string2比string1长,并且.IndexOf实际上找到了一个空格。

以下代码应该找到string1长度后第一个空格的索引

int Length_Str1 = string1.Length;

string2 = string2.Remove(string2.IndexOf(' ', Length_Str1));
var String1 = "1.1)The Element is";
var String2 = "1.1)The Element is:(-) for the sub";
var result = string.Empty;
if(String2.Contains(String1))
{
    result = String1 + Regex.Match(String2.Replace(String1, string.Empty), "[^\\sa-zA-Z0-9]+").ToString();
}

//result will contain String1 + ":(-)" from String2 IF there is a match

请注意,您还应该添加一些检查,以确保string2比string1长,并且.IndexOf实际上找到了一个空格。

这可以用正则表达式很容易地解决:

var String1 = "1.1)The Element is";
var String2 = "1.1)The Element is:(-) for the sub";
var result = string.Empty;
if(String2.Contains(String1))
{
    result = String1 + Regex.Match(String2.Replace(String1, string.Empty), "[^\\sa-zA-Z0-9]+").ToString();
}

//result will contain String1 + ":(-)" from String2 IF there is a match
//Note the special escape character here for the regex engine not to fail on a found ')'
string string1 = @"1.1\)The Element is:";

List<string> testStrings = new List<string>();
testStrings.Add(@"1.1)The Element is:(-) for the sub 1");
testStrings.Add(@"1.1)The Element is:) for the sub 2");
testStrings.Add(@"1.1)The Element is:[-] for the sub 3");

//Create a regular expression string based upon the 'string1' provided above.
string regularExpression = string.Format(@"(?<base>{0})+(?:[^\\sa-zA-Z0-9]+)", string1);
Regex regex = new Regex(regularExpression, RegexOptions.Multiline);
//Will contain the found results
List<string> subStrings = new List<string>();

foreach (string str in testStrings)
{
  foreach (Match match in regex.Matches(str))
  {
    if (match.Success)
    {
      subStrings.Add(str.Replace(match.Groups[0].ToString(), string.Empty));
    }
  }
}
//Display the found results
foreach (string str in subStrings)
{
  Console.WriteLine(str);
}
我不确定这是否是您正在寻找的,但此代码使它能够在“is:”之后具有不同的模式

编辑:
Hash Sling Slasher只是提供了相同的答案,尽管有点小:)

这可以用正则表达式很容易地解决:

//Note the special escape character here for the regex engine not to fail on a found ')'
string string1 = @"1.1\)The Element is:";

List<string> testStrings = new List<string>();
testStrings.Add(@"1.1)The Element is:(-) for the sub 1");
testStrings.Add(@"1.1)The Element is:) for the sub 2");
testStrings.Add(@"1.1)The Element is:[-] for the sub 3");

//Create a regular expression string based upon the 'string1' provided above.
string regularExpression = string.Format(@"(?<base>{0})+(?:[^\\sa-zA-Z0-9]+)", string1);
Regex regex = new Regex(regularExpression, RegexOptions.Multiline);
//Will contain the found results
List<string> subStrings = new List<string>();

foreach (string str in testStrings)
{
  foreach (Match match in regex.Matches(str))
  {
    if (match.Success)
    {
      subStrings.Add(str.Replace(match.Groups[0].ToString(), string.Empty));
    }
  }
}
//Display the found results
foreach (string str in subStrings)
{
  Console.WriteLine(str);
}
我不确定这是否是您正在寻找的,但此代码使它能够在“is:”之后具有不同的模式

编辑:

Hash Sling Slasher只是提供了相同的答案,尽管有点小:)

所以基本上,您想要得到
1.1)元素是:(-)
?您可能想要查看字符串。Substring@Tijesunimi对但这只是一个例子,我有更多的字符串。所以基本上,你想得到
1.1)元素是:(-)
?你可能想看看字符串。Substring@Tijesunimi对但这只是一个例子,我有更多的字符串。情况并非总是这样,有些字符串包含“(-”)。有些包含“)”有些包含“:”有些包含“-”。我需要一些方法来不断获取字符,直到找到一个空格。您的逻辑有缺陷-空格甚至在
元素
元素
,等等之后-您计划如何区分它们?一旦找到匹配项,就应该开始查找空格。找到什么匹配项?将string1与string2匹配,现在找到匹配字符串后的第一个空格。情况并非总是这样,有些字符串包含“(-”)。有些包含“)”有些包含“:”有些包含“-”。我需要一些方法来不断获取字符,直到找到一个空格。您的逻辑有缺陷-空格甚至在
元素
元素
,等等之后-您计划如何区分它们?一旦找到匹配项,就应该开始查找空格。找到什么匹配项?将string1与string2匹配,现在找到匹配字符串后的第一个空格。这一行有一个错误,var takeChars=rest。其中(c=>!Char.isleter(c)&&!Char.IsWhiteSpace(c));错误是:无法将lambda表达式转换为类型“System.Func”,因为它不是委托type@Kami:我已使用您的示例字符串进行了测试。您使用的是什么框架版本,是否添加了使用System.Linq的
?是的,我添加了库。但是让我再次检查一下。我在这一行上有一个错误,var takeChars=rest.Where(c=>!Char.isleter(c)&&!Char.IsWhiteSpace(c));错误是:无法将lambda表达式转换为类型“System.Func”,因为它不是委托type@Kami:我已使用您的示例字符串进行了测试。您使用的是什么框架版本,是否添加了使用System.Linq的
?是的,我添加了库。但是让我再检查一次。+1因为你的想法和我的一样,至少你的答案更快了。哈哈。我花了太多时间来解释一切(+1和我的想法一样,至少你的答案更快,哈哈。我花了太多的时间来解释一切…:(