Avinash Jadhav
Avinash Jadhav

Reputation: 1393

Separate sub string from given String in C#

I am having array of strings lets say

{"Item Revision","Integer Item Revision","Double Item Revision"} .

And I just want to exclude the "revision" part of strings .

Lets say If I am having "Integer Item Revision" string then I just want "Integer Item". Here "Revision" part is common for the all the strings. Is there any solution for it?

Upvotes: 2

Views: 110

Answers (4)

Backs
Backs

Reputation: 24903

var array = new[] {"Item Revision","Integer Item Revision","Double Item Revision"};

array = array.Select(o=>o.Replace("Revision",string.Empty).Trim()).ToArray();

OR

var array = new[] {"Item Revision NotRevision"};
Regex rgx = new Regex(@"\bRevision\b");
array = array.Select(o=>rgx.Replace(o,string.Empty).Trim()).ToArray();

Upvotes: 7

jitendragarg
jitendragarg

Reputation: 957

An example fiddle. https://dotnetfiddle.net/vwJBkn

Using string.replace will work, if you know exactly what string you want to remove.

string[] array = new[] {"Item Revision","Integer Item Revision","Double Item Revision"};
    foreach (string i in array)
    {
//Replace console.writeline with your code. 
        Console.WriteLine (i.Replace("Revision", String.Empty));
    }

Upvotes: 3

D T
D T

Reputation: 3746

Can use for:

 String[] s = { "Item Revision", "Integer Item Revision", "Double Item Revision" };
            String[] tmp = new String[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                tmp[i] = s[i].Replace("Revision", "");
            }

Upvotes: 4

Yatrix
Yatrix

Reputation: 13775

var result = "Item Revision".Replace(" Revision", "");

That'll strip away "Revision" and the leading space.

Upvotes: 4

Related Questions