user3924730
user3924730

Reputation: 183

How to Get array values after a specific text

I have an array which is made from the following string..

FixedLines Rs. 0.200 / 1 min Rs. 0.600 / 1 min

I converted this string into string array by splitting based on the spaces in the string. Now i am trying to get the string after substring Rs. means i want the values 0.200 / 1 min and 0.600 / 1 min.

Please help me how can i get these values from the string array into string.

Upvotes: 0

Views: 67

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460138

You could split by Rs. and take only the parts which contain /:

string str = "FixedLines Rs. 0.200 / 1 min Rs. 0.600 / 1 min";
var parts = str.Split(new[] { " Rs. " }, StringSplitOptions.RemoveEmptyEntries)
    .Where(p => p.Contains("/"));

foreach(string part in parts)
    Console.WriteLine(part);

Outputs:

0.200 / 1 min
0.600 / 1 min

You:

How to get these two values into string?

You can use string.Join, f.e. if you want to separate them with new-line characters:

string values = string.Join(Environment.NewLine, parts);

You:

I am sorry ! but i want into two string for example str1 and str2

I suggest to use a collection instead of single variables or are you sure that there are always only two values?

You can use ToArray or ToList to create a collection, then you can use the indexer or LINQ extension method like First,Last or ElementAtOrDefault:

string[] values = parts.ToArray();
string firstVal = values.First();
string lastVal = values.Last();

Upvotes: 1

Tom
Tom

Reputation: 2611

You can do what Sayse said in the comments to your question.

        string phrase = "FixedLines Rs. 0.200 / 1 min Rs. 0.600 / 1 min";

        var split = new string[1];
        split[0] = "Rs.";

        var results = phrase.Split(split, StringSplitOptions.RemoveEmptyEntries).Skip(1);

Upvotes: 1

Sayse
Sayse

Reputation: 43300

It sounds like you could just do

 stringName.Split(new string[]{" Rs. "}, StringSplitOptions.RemoveEmptyEntries)
           .Skip(1);

Split the string based on Rs. and then skip the first entry

Upvotes: 0

Related Questions