2

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?

2
  • Is "Revision" is constant? or are you first looking for which word is common ? Commented Apr 29, 2016 at 4:56
  • @codetoshare yes you can say "Revision" is constant part Commented Apr 29, 2016 at 4:57

4 Answers 4

7
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();
Sign up to request clarification or add additional context in comments.

8 Comments

how do you find the intersecting string "revision"? This might be dynamic, rather than fixed string.
@David read comment from the person who asked the question. It is fixed.
@David for more complicated cases - regex with \b around the word
@jitendragarg, thanks. Just curious to know, if this could be a more general solution.
Depends on the requirement then. Is it the common part in all strings? Is it supposed to be a database value? Is it supposed to come as a parameter? Are we supposed to calculate it using some logic? Answer will change based on the how we want to find that string. :/
|
4
var result = "Item Revision".Replace(" Revision", "");

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

2 Comments

@David at no point does OP ask to "find" it. OP wrote "I just want to exclude the "revision" part of strings".
it might be. But I am just curious if there is a simple one-line solution to find this literal string? :)
4

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", "");
            }

1 Comment

Yes, we can update original array. I only want to keep original value, so i create a tmp array. Thanks.
3

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));
    }

2 Comments

You should post the relevant part of code in the answer, just in case dotnetfiddle is down or goes away
Thanks. That is a good idea. To be honest, I myself updated the same fiddle by mistake, and had to redo it again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.