-1

How can i find an operator in the string? After that, I'd like to create get the left and the right variable.

If the string was:

DisengagementPoint=TravelForce

or

DisengagementPoint=100

but some time string be longer like:

DisengagementPoint=100 (VehicleSpeed>2) || (EngineRpm== 0)

And I want to create a string with whatever is on the left side of "=" and one string with right side, how could I do that?

Output that I expected:

var1 = DisengagementPoint

var2 = 100 or TravelForce

var3 = (VehicleSpeed>2) || (EngineRpm== 0)

Sorry this is pretty pseudo, but hopefully it makes sense.

  • 4
    You could use `string.Split('=')` to get an array. Then `strArr[0]` is the left and `strArr[1]` is the right word. – Tim Schmelter Feb 16 '16 at 14:51
  • Is `=` the only operator you care about? How do you want to handle cases where it occurs more than once? – Dan Field Feb 16 '16 at 14:54
  • for the variable between "(" ")" i use an interpreter – Adrian Moldovan Feb 16 '16 at 14:58
  • i think first i need to take out what is between () and make 2 string one with `DisengagementPoint=100` and one `(VehicleSpeed>2) || (EngineRpm== 0)` – Adrian Moldovan Feb 16 '16 at 15:03
  • @AdrianMoldovan: Why don't you update your question with expected ouput alongside the input? – huMpty duMpty Feb 16 '16 at 15:05
  • @AdrianMoldovan This relates to the question you deleted of find a list of strings in a string (list) and might assist you http://stackoverflow.com/questions/5456577/linq-return-items-in-a-list-that-matches-any-names-string-in-another-list – Mark Schultheiss Feb 17 '16 at 15:06
  • I recommend throwing an existing parser for [context free grammars](https://en.wikipedia.org/wiki/Context-free_grammar) at the problem. Or at least study techniques for parsing such grammars. – CodesInChaos Apr 20 '16 at 08:06

3 Answers3

2

I would like add two things to other excelent answers:

A) The string.Split splits the string by all equals thus DisengagementPoint=100 would go out just fine but line = "DisengagementPoint=100 (VehicleSpeed>2) || (EngineRpm== 0)" would get quite messy since just splitting with string string[] split = line.Split('=') would yield array:

split[0] = "DisengagementPoint"
split[1] = "100 (VehicleSpeed>2) || (EngineRpm"
split[2] = ""
split[3] = " 0)"

This can be eliminated by restricting maximal number of splits with line.Split('=', 2) returns maximally two sub-strings. And also line.Split('=', StringSplitOptions.RemoveEmptyEntries) could help to get rid if empty parts, but this would not be an issue in this case.

B) What with problem of comparison operator ==? Perheaps check if there are only a single equals or more of them and if more, if there are at least two of them right behind themselves.

As mentioned in comment to @Dmitry Bychenko´s post by @AustinWBryan this starts to look parser problem, which is a bit different question in my opinion.

Rao
  • 802
  • 8
  • 19
1

Just find = position and take substrings:

String source = "DisengagementPoint=100";

int index = source.IndexOf('=');

// you may want to test if there's '=' in the string
// if (index >= 0) ...   

String left = source.Substring(0, index);
String right = source.Substring(index + 1);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • This doesn't work for his later example of `DisengagementPoint=100 (VehicleSpeed>2) || (EngineRpm== 0)` – AustinWBryan Feb 16 '16 at 14:59
  • @AustinWBryan: what's the expected output for the `"DisengagementPoint=100 (VehicleSpeed>2) || (EngineRpm== 0)"`? My current code'll return `DisengagementPoint` and `100 (VehicleSpeed>2) || (EngineRpm== 0)` – Dmitry Bychenko Feb 16 '16 at 15:01
  • first i think i need to split the string in 2 one with `DisengagementPoint=100` and one with `(VehicleSpeed>2) || (EngineRpm== 0)` – Adrian Moldovan Feb 16 '16 at 15:04
  • It's not very clear, but I believe he wants some expression tree, where all of the terms are stored by themselves; `str[0] = "DisengagementPoint", str[1] = "100", str[2] = "VechicleSpeed"`, etc., while maintining the order of operations, basically a parse – AustinWBryan Feb 16 '16 at 15:05
1
var word = "DisengagementPoint=TravelForce";
var index = word.IndexOf("=");

if(index > 1)
{
    var left  = word.Substring(0, index);
    var right = word.Substring(index + 1,word.Length - index - 1);
}

As DmitryBychenko mentioned

var right = word.Substring(index + 1,word.Length - index - 1);

can be replaced with

var right = word.Substring(index + 1);
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99