繁体   English   中英

字符串操作和拆分字符串

[英]String Manipulation and splitting string

string url = "test:app:https://test@hotmail.co.uk:Test

我需要将其拆分以显示如下

string first = "app";
string second = "https://test@hotmail.co.uk:Test";

我尝试了以下操作,但在最后一个冒号上失败了。

        string remove= "";
        remove= url.Replace("test:", "");
        string first= remove.Substring(remove.LastIndexOf(':') + 1);
        string second= remove.Substring(0, remove.IndexOf(':'));

这样做我得到

first = "app";
second = "Test";

当我需要

first = "app";
second = "https://test@hotmail.co.uk:Test";

您对LastIndexOf使用有点不稳定。

string url = "test:app:https://test@hotmail.co.uk:Test";

string remove = url.Replace("test:", "");

string first = remove.Substring(0, remove.IndexOf(":"));
string second = remove.Substring(remove.IndexOf(first) + first.Length + 1);

首先抓取app ,我们可以使用app的位置来导出字符串的其余部分。 因为最后一个索引:将是一个在:Test 我们不希望的最后一个索引: 相反,我们只想要app之后的任何内容。

由于所有内容都以test:为前缀,您可以在此之后使用起始位置,然后在:字符第一次出现后拆分。

const int IndexOfPrefix = 5; // start position after "test:"
string url = "test:app:https://test@hotmail.co.uk:Test";
var indexOfApp = url.IndexOf(':', IndexOfPrefix);
var part1 = url.Substring(IndexOfPrefix, indexOfApp - IndexOfPrefix);
var part2 = url.Substring(indexOfApp + 1);

Console.WriteLine(part1);
Console.WriteLine(part2);

您可以使用Split(Char[], Int32)获得所需数量的元素(3:第一个不需要的部分,第一个预期的部分和其余部分)以及Skip()删除不需要的元素:

string url = "test:app:https://test@hotmail.co.uk:Test";

var splitted = url.Split(new [] { ':' }, 3).Skip(1).ToArray();
var first = splitted[0];
var second = splitted[1];

Console.WriteLine(first);
Console.WriteLine(second);

这输出

app
https://test@hotmail.co.uk:Test

另一种方法是使用正则表达式:

模式:(?<first>.*?):(?<second>.*)将:

  • :搜索字符:
  • (?<first>.*?)创建一个名为first的组,该组将匹配任意数量的任何字符(懒惰)
  • :搜索字符:
  • (?<second>.*)创建一个名为second的组,它将匹配任意数量的任何字符(贪婪)

例如:

string url = "test:app:https://test@hotmail.co.uk:Test";
var pattern = ":(?<first>.*?):(?<second>.*)";
var regex = new Regex(pattern); // using System.Text.RegularExpressions;
Match match = regex.Match(url);
if (match.Success)
{
    var first = match.Groups["first"].Value;
    var second = match.Groups["second"].Value;
    Console.WriteLine(first);
    Console.WriteLine(second);
}

这输出

app
https://test@hotmail.co.uk:Test

像这样的事情应该可以解决问题:

public void ManipulateStrings()
{
    string url = "test:app:https://test@hotmail.co.uk:Test";
    url = url.Replace("test:", "");
    string first = url.Substring(0, url.IndexOf(':'));
    string second = url.Substring(url.IndexOf(':') + 1);

}

这基本上删除了test:从您的字符串中,然后分配第一个和第二个它们的值,而无需无缘无故地创建string remove = ""

您需要将名称为“first”的变量更改为“second”,并将名称为“second”的变量更改为“first”

这是你的代码:

string url = "test:app: https://test@hotmail.co.uk:Test ";

        string remove = "";
        remove = url.Replace("test:", "");
        string second = remove.Substring(0, remove.IndexOf(':'));
        string first = remove.Substring(remove.IndexOf(":") + 1);

这是正确的代码:

string url = "test:app: https://test@hotmail.co.uk:Test ";

        string remove = "";
        remove = url.Replace("test:", "");
        string first = remove.Substring(0, remove.IndexOf(':'));
        string second = remove.Substring(remove.IndexOf(":") + 1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM