简体   繁体   English

字符串操作和拆分字符串

[英]String Manipulation and splitting string

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

I need to split this up to display as follows我需要将其拆分以显示如下

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

i have tried the following but falls over on the last colon.我尝试了以下操作,但在最后一个冒号上失败了。

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

Doing this i get这样做我得到

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

When i need当我需要

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

Your use of LastIndexOf is just a bit wonky.您对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);

First grab the app , and we can use the location of app to derive the rest of the string.首先抓取app ,我们可以使用app的位置来导出字符串的其余部分。 Because the last index of : would be the one in :Test .因为最后一个索引:将是一个在:Test We don't want the last index of : .我们不希望的最后一个索引: Instead we just want whatever comes after app .相反,我们只想要app之后的任何内容。

As everything is prefixed with test: you can use a starting position after that and then split after the first occurrance of the : character.由于所有内容都以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);

You can use Split(Char[], Int32) to get the desired number of elements (3 : the first unwanted part, the first expected part and the remaining) along with Skip() to remove the unwanted one :您可以使用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);

This outputs这输出

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

Another way to do that is using regular expressions :另一种方法是使用正则表达式:

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

  • : search for the characters : :搜索字符:
  • (?<first>.*?) creates a group named first that will match any number of any character (lazy) (?<first>.*?)创建一个名为first的组,该组将匹配任意数量的任何字符(懒惰)
  • : search for the characters : :搜索字符:
  • (?<second>.*) creates a group named second that will match any number of any character (greedy) (?<second>.*)创建一个名为second的组,它将匹配任意数量的任何字符(贪婪)

In example :例如:

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);
}

This outputs这输出

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

Something like this should do the trick:像这样的事情应该可以解决问题:

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);

}

This basically removed test: from your string, then assigns first and second their values without creating string remove = "" for no reason.这基本上删除了test:从您的字符串中,然后分配第一个和第二个它们的值,而无需无缘无故地创建string remove = ""

you need to change the variable with name "first" to "second" and to change the variable with name "second" to "first"您需要将名称为“first”的变量更改为“second”,并将名称为“second”的变量更改为“first”

This is your code:这是你的代码:

string url = "test:app: https://test@hotmail.co.uk:Test "; 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);

and this is the correct code:这是正确的代码:

string url = "test:app: https://test@hotmail.co.uk:Test "; 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