Split a plus line as a delimiter

I have a problem with a line containing a plus sign (+). I want to break this line (or if there is another way to solve my problem)

string ColumnPlusLevel = "+-J10+-J10+-J10+-J10+-J10";
string strpluslevel = ""; 
strpluslevel = ColumnPlusLevel; 
string[] strpluslevel_lines = Regex.Split(strpluslevel, "+");

foreach (string line in strpluslevel_lines)
{
    MessageBox.Show(line);
    strpluslevel_summa = strpluslevel_summa + line;
}   

MessageBox.Show(strpluslevel_summa, "summa sumarum");

MessageBox is for my testing purpose.

Now ... A ColumnPlusLevel string can have a very diverse entry, but it is always a repeating pattern starting with a plus sign. for example, "+ MJ + MJ + MJ" or "+ PPL14.1 + PPL14.1 + PPL14.1". (It is part of other software, and I cannot edit the output from this software)

How can I find out that this pattern is repeating? In this example, there is + -J10 or + MJ or + PPL14.1

, MessageBox, , , pattering .

, , Split, , . , .

, , .

.

/Tomas

+4
4

, ?

, , :

string[] tokens = ColumnPlusLevel.Split(new[]{'+'}, StringSplitOptions.RemoveEmptyEntries);
string first = tokens[0];
bool repeatingPattern = tokens.Skip(1).All(s => s == first);

repeatingPattern - true, , first.

, ?

, tokens.Skip(1), LINQ, using System.Linq . tokens string[], IEnumerable<string>, LINQ (extension-). Enumerable.Skip(1) , , , . All, false, ( ). , , , first.

+5

String.Split:

string pattern = ColumnPlusLevel.Split("+")[0];
+1

... , .

String.Split() , ?

string input = @"+MJ+MJ+MJ";

int indexOfSecondPlus = input.IndexOf('+', 1);
string pattern = input.Remove(indexOfSecondPlus, input.Length - indexOfSecondPlus);
//pattern is now "+MJ"

, LinQ

0

Stringhas a method called Splitthat allows you to split / split a string based on a given character / character set:

string givenString = "+-J10+-J10+-J10+-J10+-J10"'

string SplittedString = givenString.Split("+")[0] ///Here + is the character based on which the string would be splitted and 0 is the index number

string result = SplittedString.Replace("-","") //The mothod REPLACE replaces the given string with a targeted string,i added this so that you can get the numbers only from the string
0
source

Source: https://habr.com/ru/post/1695529/


All Articles