4

I have an issue with a string containing the plus sign (+). I want to split that string (or if there is some other 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");

The MessageBox is for my testing purpose.

Now... The ColumnPlusLevel string can have very varied entry but it is always a repeated pattern starting with the plus sign. i.e. "+MJ+MJ+MJ" or "+PPL14.1+PPL14.1+PPL14.1" as examples. (It comes form Another software and I cant edit the output from that software)

How can I find out what that pattern is that is being repeated? That in this exampels is the +-J10 or +MJ or +PPL14.1

In my case above I have tested it by using only a MessageBox to show the result but I want the repeated pattering stored in a string later on.

Maybe im doing it wrong by using Split, maybe there is another solution. Maybe I use Split in the wrong way.

Hope you understand my problem and the result I want.

Thanks for any advice.

/Tomas

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • 2
    Please post a [mcve]. You did post code, but I cannot tell *what exactly* you want to achieve. Do you like what's presented in your message boxes? If not, why not? What do you *want*? – nvoigt Mar 29 '18 at 12:34
  • Please be more specific on the input and desired output; it is a bit unclear from the current formulation of the question. – Codor Mar 29 '18 at 12:34
  • Why does everybody want to use Regex for simple problems that can use string method : string[] strpluslevel_lines = strpluslevel.Split(new char[] { '+'}); – jdweng Mar 29 '18 at 12:34
  • What exactly is the problem with your code. What isn't working? – Jerodev Mar 29 '18 at 12:35
  • You may be able to use regex to determine the shortest repeating pattern: https://stackoverflow.com/questions/28963384/finding-the-shortest-repetitive-pattern-in-a-string – user1934587390 Mar 29 '18 at 12:41
  • Will you need to know how often the pattern repeated? – Fildor Mar 29 '18 at 12:44

4 Answers4

5

How can I find out what that pattern is that is being repeated?

Maybe i didn't understand the requirement fully, but isn't it easy as:

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

If repeatingPattern is true you know that the pattern itself is first.

Can you maybe explain how the logic works

The line which contains tokens.Skip(1) is a LINQ query, so you need to add using System.Linq at the top of your code file. Since tokens is a string[] which implements IEnumerable<string> you can use any LINQ (extension-)method. Enumerable.Skip(1) will skip the first because i have already stored that in a variable and i want to know if all others are same. Therefore i use All which returns false as soon as one item doesn't match the condition(so one string is different to the first). If all are same you know that there is a repeating pattern which is already stored in the variable first.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thank you very much. Can you maybe explain how the logic works in the Bool line. Might be good to know if any tweaking is needed later on. :-) – Tomas Nordström Mar 29 '18 at 12:49
1

You should use String.Split function :

string pattern = ColumnPlusLevel.Split("+")[0];
Onyx Caldin
  • 289
  • 1
  • 12
0

...but it is always a repeated pattern starting with the plus sign.

Why do you even need String.Split() here if the pattern always only repeats itself?

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

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

No need of string split, no need to use LinQ

Chrᴉz remembers Monica
  • 1,829
  • 1
  • 10
  • 24
0

String has a method called Split which let's you split/divide the 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
Software Dev
  • 5,368
  • 5
  • 22
  • 45