如何仅在最后出现特殊字符时拆分字符串并在拆分后使用两侧

How do I Split string only at last occurrence of special character and use both sides after split

我只想在最后出现的特殊字符处拆分字符串。

我尝试从浏览器解析选项卡的名称,因此我的初始字符串看起来像这样:

Untitled - Google Chrome

因为有拆分功能,所以很容易解决。这是我的实现:

var pageparts= Regex.Split(inputWindow.ToString(), " - ");
InsertWindowName(pageparts[0].ToString(), pageparts[1].ToString());//method to save string into separate columns in DB

这有效,但是当我得到这样的页面时出现问题:

SQL injection - Wikipedia, the free encyclopedia - Mozilla Firefox

这里有两个破折号,这意味着拆分完成后,数组中有 3 个单独的字符串,如果我正常继续,数据库将包含第一列值 "SQL injection" 和第二列值"Wikipedia, the free encyclopedia"。最后一个值将被完全排除在外。

我想要的是数据库中的第一列将具有值: SQL injection - Wikipedia, the free encyclopedia" 和第二栏将有: "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.SubstringString.LastIndexOf 一起使用:

string str = "SQL injection - Wikipedia, the free encyclopedia - Mozilla Firefox";
int lastIndex = str.LastIndexOf('-');
if (lastIndex + 1 < str.Length)
{
    string firstPart = str.Substring(0, lastIndex);
    string secondPart = str.Substring(lastIndex + 1);
}

创建扩展方法 (或简单方法) 来执行该操作并为 lastIndex 添加一些错误检查。

编辑:

如果要在 " - " (space-space) 上拆分,则使用以下方法计算 lastIndex

string str = "FirstPart - Mozzila Firefox-somethingWithoutSpace";
string delimiter = " - ";
int lastIndex = str.LastIndexOf(delimiter);
if (lastIndex + delimiter.Length < str.Length)
{
    string firstPart = str.Substring(0, lastIndex);
    string secondPart = str.Substring(lastIndex + delimiter.Length);
}

所以对于像这样的字符串:

"FirstPart - Mozzila Firefox-somethingWithoutSpace"

输出将是:

FirstPart 
Mozzila Firefox-somethingWithoutSpace

请原谅我在这个解决方案中的懒惰,我确信有更好的方法,但我会给你一个解决方案建议,我假设你正在用 C# 编码。 首先,如果我弄错了问题,请纠正我,无论您只想返回第一个列(所有文本甚至包括破折号,但最后一个)和最后一列(最后一个破折号之后的所有文本)如果没问题。我们开始做吧。 // 当我希望所有数据都在单独的变量(数组位置)中时,我只使用拆分函数在你的情况下我假设你只想要 2 个值(如果可能的话),所以你可以使用子字符串。

    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 injection - Wikipedia,- the free - encyclopedia - Mozilla Firefox" 应该返回什么? 请原谅我今天无聊的代码。