C# 如何仅在最后一次出现特殊字符时分割字符串,并在分割后使用两侧

C# 如何仅在最后一次出现特殊字符时分割字符串,并在分割后使用两侧,c#,regex,wpf,string,split,C#,Regex,Wpf,String,Split,我只想在最后一次出现特殊字符时分割字符串 我试图从浏览器中解析选项卡的名称,因此我的初始字符串如下所示: 无标题-谷歌浏览器 这很容易解决,因为有一个分割函数。以下是我的实现: var pageparts= Regex.Split(inputWindow.ToString(), " - "); InsertWindowName(pageparts[0].ToString(), pageparts[1].ToString());//method to save string into separa

我只想在最后一次出现特殊字符时分割字符串

我试图从浏览器中解析选项卡的名称,因此我的初始字符串如下所示:

无标题-谷歌浏览器

这很容易解决,因为有一个分割函数。以下是我的实现:

var pageparts= Regex.Split(inputWindow.ToString(), " - ");
InsertWindowName(pageparts[0].ToString(), pageparts[1].ToString());//method to save string into separate columns in DB
这是可行的,但当我得到这样一个页面时,问题就出现了:

SQL注入-维基百科,免费百科全书-Mozilla Firefox

这里有两个破折号,这意味着在拆分完成后,数组中有3个单独的字符串,如果我继续正常操作,数据库将包含在第一列值SQL injection和第二列值Wikipedia(免费百科全书)中。最后一个值将被完全忽略

我希望数据库中的第一列具有以下值: SQL注入-免费百科全书Wikipedia和第二专栏将包含: Mozilla Firefox。这有可能吗


我尝试使用Split-。Last函数甚至LastOrDefault,但我只得到了最后一个字符串。我需要得到原始字符串的两边。仅由最后一个破折号分隔。

如果您不关心重新组合字符串,可以使用以下方法:

var pageparts= Regex.Split(inputWindow.ToString(), " - ");
var firstPart = string.Join(" - ", pageparts.Take(pageparts.Length - 1));
var secondPart = pageparts.Last()

InsertWindowName(firstPart, secondPart);
可以将String.Substring与String.LastIndexOf一起使用:

所以对于类似字符串的:

"FirstPart - Mozzila Firefox-somethingWithoutSpace"
产出将是:

FirstPart 
Mozzila Firefox-somethingWithoutSpace

请原谅我在这个解决方案中的懒惰,我相信有更好的方法,但我会给你一个解决方案建议,我假设你正在编写C。 首先,纠正我,如果我得到错误的问题,无论你只是想列返回的第一个所有文本,即使它包括破折号,但最后一个和最后一列所有文本后,最后一个破折号,如果它是确定的。让我们做吧。 //我只在希望所有数据都位于单独的变量数组位置时才使用split函数。如果可能的话,我假设您只需要2个值,所以可以使用substring

    static void Main(string[] args)
    {

        string firstname = "";
        string lastName = "";
        string variablewithdata = "SQL injection - Wikipedia, -the free encyclopedia - Mozilla Firefox";

       // variablewithdata.LastIndexOf('-') = returns Integer corresponding to the last position of that character.
        //I suggest you validate if variablewithdata.LastIndexOf('-') is equal to -1 or not because if it don't found your character it returns -1 so if the value isn't -1 you can substring

        firstname = variablewithdata.Substring(0, (variablewithdata.LastIndexOf('-') - 1));
        lastName = variablewithdata.Substring(variablewithdata.LastIndexOf('-') + 1);

        Console.WriteLine("FirstColumn: {0} \nLastColumn:{1}",firstname,lastName);
        Console.ReadLine();
    }
如果这不是你想要的,你能给我解释一下比如SQL注入-维基百科-免费百科全书-Mozilla Firefox应该返回什么吗?
请原谅我不干净的代码,我今天很无聊。

我已经编辑了你的标题。请看,如果共识是否定的,他们就不应该这样。对此我真的很抱歉,我下次会知道的。谢谢你的链接,你不必道歉。没什么大不了的。现在,你知道了,你的解决方案不适用于像FirstPart-Mozzila Firefox这样的东西-somethingWithoutSpace@Seb,对于该示例,它将非常有效,因为不需要修剪:@Clemens,可能是,我没有使用它,因为OP在分割结果上也没有使用修剪。@Seb,从问题中,我尝试使用一个拆分-。最后一个函数,我相信OP希望拆分-,而不考虑空间。@Seb,即使使用像FirstPart-Mozzila-Firefox-something这样的字符串,OP可以使用上面的代码替换LastIndexOf-使用空格,它将返回FirstPart和Mozzila Firefox,但不返回空格。我不在乎重新组装。我只关心拆分该字符串并将这两个值保存到数据库中。我将如何利用您提供的代码?我不明白,它是如何与我提供的代码一起工作的。你能给我解释一下吗?
FirstPart 
Mozzila Firefox-somethingWithoutSpace
    static void Main(string[] args)
    {

        string firstname = "";
        string lastName = "";
        string variablewithdata = "SQL injection - Wikipedia, -the free encyclopedia - Mozilla Firefox";

       // variablewithdata.LastIndexOf('-') = returns Integer corresponding to the last position of that character.
        //I suggest you validate if variablewithdata.LastIndexOf('-') is equal to -1 or not because if it don't found your character it returns -1 so if the value isn't -1 you can substring

        firstname = variablewithdata.Substring(0, (variablewithdata.LastIndexOf('-') - 1));
        lastName = variablewithdata.Substring(variablewithdata.LastIndexOf('-') + 1);

        Console.WriteLine("FirstColumn: {0} \nLastColumn:{1}",firstname,lastName);
        Console.ReadLine();
    }