简体   繁体   English

字符串拆分为不带分隔符的指定字符串

[英]String split with specified string without delimeter

Updated - When searched value is in middle

string text = "Trio charged over alleged $100m money laundering syndicate at Merrylands, Guildford West";
string searchtext= "charged over";
string[] fragments = text.Split(new string[] { searchtext }, StringSplitOptions.None);

    //Fragments
   //if [0] is blank searched text is in the beginning - searchedtext + [1]
  //if [1] is blank searched text is in the end - [0] + searched text
  // If searched text is in middle then both items has value - [0] + seachedtext + [1]

 //This loop will execute only two times because it can have maximum 2 values, issue will
 //come when searched value is in middle (loop should run 3 times) as for the searched value i have to apply differnt logic (like change background color of the text)
 // and dont change background color for head and tail
 //How do i insert searched value in middle of [0] and [1] ??

I am having a string without delimeter which i am trying to split based on searched string.我有一个没有定界符的字符串,我试图根据搜索到的字符串进行拆分。 My requirement is split the string into two, one part contains string without the searchtext and other contains searchtext like below-我的要求是将字符串分成两部分,一部分包含没有搜索文本的字符串,另一部分包含搜索文本,如下所示 -

 Original String - "Bitcoin ATMs Highlight Flaws in EU Money Laundering Rules"
    String 1 - Bitcoin ATMs Highlight Flaws in EU 
    String 2 - Money Laundering Rules

I have written below code it works for the above sample value, but it failed for 

Failed - Not returning String 1 and String 2, String is empty
string watch = " Money Laundering Rules Bitcoin ATMs Highlight Flaws in EU";
string serachetxt = "Money Laundering Rules";

This works -这有效-

List<string> matchedstr = new List<string>();
string watch = "Bitcoin ATMs Highlight Flaws in EU Money Laundering Rules";
string serachetxt = "Money Laundering Rules";

string compa = watch.Substring(0,watch.IndexOf(serachetxt)); //It returns "Bitcoin ATMs Highlight Flaws in EU"

matchedstr.Add(compa);
matchedstr.Add(serachetxt);

foreach(var itemco in matchedstr)
{

} }

You could just consider "Money Laundering Rules" to be the delimiter.您可以将"Money Laundering Rules"视为分隔符。 Then you can write然后你可以写

string[] result = watch.Split(new string[] { searchtext }, StringSplitOptions.None);

Then you can add the delimiter again然后你可以再次添加分隔符

string result1 = result[0];
string result2 = searchtext + result[1];

Use string.Split.使用 string.Split。

string text = "Bitcoin ATMs Highlight Flaws in EU Money Laundering Rules";
string searchtext = "Money Laundering Rules";
string[] fragments = text.Split(new string[] { searchtext }, StringSplitOptions.None);

fragments will equal: fragments将等于:

[0] "Bitcoin ATMs Highlight Flaws in EU "
[1] ""

Everywhere there is a gap between consecutive array elements, your search string appears.连续数组元素之间的任何地方都会出现您的搜索字符串。 eg:例如:

string originaltext = string.Join(searchtext, fragments);

Extended Description of String.Split Behaviour String.Split 行为的扩展描述

Here is a quick table of the behaviour of string.Split when passed a string.这是 string.Split 传递字符串时行为的快速表。

| Input  | Split | Result Array       |
+--------+-------+--------------------+
| "ABC"  | "A"   | { "", "BC" }       |
| "ABC"  | "B"   | { "A", "C" }       |
| "ABC"  | "C"   | { "AB", "" }       |
| "ABC"  | "D"   | { "ABC" }          |
| "ABC"  | "ABC" | { "", "" }         |
| "ABBA" | "A"   | { "", "BB", "" }   |
| "ABBA" | "B"   | { "A", "", "A" }   |
| "AAA"  | "A"   | { "", "", "", "" } |
| "AAA"  | "AA"  | { "", "A" }        |

If you look at the table above, Every place there was a comma in the array (between two consecutive elements in the array), is a place that the split string was found.如果您查看上表,数组中每个有逗号的地方(数组中两个连续元素之间),都是找到拆分字符串的地方。

If the string was not found, then the result array is only one element (the original string).如果未找到字符串,则结果数组只有一个元素(原始字符串)。

If the split string is found at the beginning of the input string, then an empty string is set as the first element of the result array to represent the beginning of the string.如果在输入字符串的开头找到拆分字符串,则将空字符串设置为结果数组的第一个元素以表示字符串的开头。 Similarly, if the split string is found at the end of the string, an empty string is set as the last element of the result array.同样,如果在字符串末尾找到拆分字符串,则将空字符串设置为结果数组的最后一个元素。

Also, an empty string is included between any consecutive occurrences of the search string in the input string.此外,在输入字符串中任何连续出现的搜索字符串之间包含一个空字符串。

In cases where there are ambiguous overlapping locations at which the string could be found in the input string: (eg splitting AAA on AA could be split as AA | A or A | AA - where AA is found at position 0 or position 1 in the input string) then the earlier location is used.如果存在可以在输入字符串中找到字符串的模糊重叠位置:(例如,在AA上拆分AAA可以拆分为AA | AA | AA - 其中AA位于 position 0 或 position 1输入字符串)然后使用较早的位置。 (eg AA | A , resulting in { "", "A" } ). (例如AA | A ,导致{ "", "A" } )。

Again, the invariant is that the original string can always be reconstructed by joining all the fragments and placing exactly one occurrence of the search text in between elements.同样,不变的是原始字符串总是可以通过连接所有片段并将搜索文本恰好放置在元素之间的一次出现来重建。 The following will always be true:以下将始终为真:

string.Join(searchtext, fragments) == text

If you only want the first split...如果你只想要第一次分裂......

You can merge all results after the first back together like this:您可以像这样在第一次合并后合并所有结果:

if (fragments.Length > 1) {
    fragments = new string[] { fragments[0], string.Join(searchtext, fragments.Skip(1)) };
}

... or a more efficient way using String.IndexOf ...或使用String.IndexOf的更有效方法

If you just want to find the first location of the search text string then use String.IndexOf to get the position of the first occurrence of the search text in the input string.如果您只想找到搜索文本字符串的第一个位置,则使用String.IndexOf获取输入字符串中搜索文本第一次出现的 position。

Here's a complete function you can use这是您可以使用的完整 function

private static bool TrySplitOnce(string text, string searchtext, out string beforetext, out string aftertext)
{
    int pos = text.IndexOf(searchtext);
    if (pos < 0) {
        // not found
        beforetext = null;
        aftertext = null;
        return false;
    } else {
        // found at position `pos`
        beforetext = text.Substring(0, pos); // may be ""
        aftertext = text.Substring(pos + searchtext.Length); // may be ""
        return true;
    }
}

You can use this to produce an array, if you like.如果愿意,您可以使用它来生成数组。

usage:用法:

string text = "red or white or blue";
string searchtext = "or";
if (TrySplitOnce(text, searchtext, out string before, out string after)) {
    Console.WriteLine("{0}*{1}", before, after);
    // output:
    //     red * white or blue
    string[] array = new string[] { before, searchtext, after };
    // array == { "red ", "or", " white or blue" };
    Console.WriteLine(string.Join("|", array));
    // output:
    //     red |or| white or blue  
} else {
    Console.WriteLine("Not found");
}

output: output:

red * white or blue
red |or| white or blue

You can write your own extension method for this:您可以为此编写自己的扩展方法:

// Splits s at sep with sep included at beginning of each part except first
// return no more than numParts parts
public static IEnumerable<string> SplitsBeforeInc(this string s, string sep, int numParts = Int32.MaxValue)
    => s.Split(new[] { sep }, numParts, StringSplitOptions.None).Select((p,i) => i > 0 ? sep+p : p);

And use it with:并将其用于:

foreach(var itemco in watch.SplitsBeforeInc(watch, serachetxt, 2))

Here is the same method in a non-LINQ version:这是非 LINQ 版本中的相同方法:

// Splits s at sep with sep included at beginning of each part except first
// return no more than numParts parts
public static IEnumerable<string> SplitsBeforeInc(this string s, string sep, int numParts = Int32.MaxValue) {
    var startPos = 0;
    var searchPos = 0;
    while (startPos < s.Length && --numParts > 0) {
        var sepPos = s.IndexOf(sep, searchPos);
        sepPos = sepPos < 0 ? s.Length : sepPos;
        yield return s.Substring(startPos, sepPos - startPos);
        startPos = sepPos;
        searchPos = sepPos+sep.Length;
    }
    if (startPos < s.Length)
        yield return s.Substring(startPos);
}

You can try this你可以试试这个

        string text = "Trio charged over alleged $100m money laundering syndicate at Merrylands, Guildford West";
        string searchtext = "charged over";
        searchtextPattern =  "(?=" + searchtext + ")";

        string[] fragments= Regex.Split(text, searchtextPattern);
        //fargments will have two elements here
        // fragments[0] - "Trio"
        // fragments[1] - "charged over alleged $100m money laundering syndicate at Merrylands, Guildford West"

now you can again split fragment which have search text ie fragments[1] in this case.现在您可以再次拆分具有搜索文本的片段,即本例中的片段 [1]。 see code below见下面的代码

            var stringWithoutSearchText = fragments[1].Replace(searchtext, string.Empty);

you need to check whether each fragment contains search text or not.您需要检查每个片段是否包含搜索文本。 You can do that it your foreach loop on fragments.您可以在片段上执行 foreach 循环。 add below check over there在下面添加检查那里

     foreach (var item in fragments)
     { 
        if (item.Contains(searchtext))
        { 
          string stringWithoutSearchText = item.Replace(searchtext, string.Empty);
        }
     }

Reference: https://stackoverflow.com/a/521172/8652887参考: https://stackoverflow.com/a/521172/8652887

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

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