1

I have got a String with some special characters.

string strValue = "MyString[]{"; 

Here the string strValue contains some special characters and these characters are mentioned in a character array arrFindString and the corresponding character need to be replaced with the character in another character array in arrReplaceString having the same index position.

char[] findArray = {'[',']','{'};
char[] replaceArray = { '(', ')', '-' };

So in my case the string strValue has a character called '['.So at first we need to find the corresponding index of that character in findArray then we need to replace that character with the character having the same index in replaceArray .So here '[' has to be replaced by '('

Is there any way the same can be accomplished using linq?... I know its possible using for each .Please help

1
  • Yes it can be, but the foreach is far more natural and will be easier to maintain. Commented Oct 20, 2015 at 7:41

4 Answers 4

2

First, I'd rather combine findArray and replaceArray into single Dictionary<Char, Char>:

  Dictionary<Char, Char> replaces = new Dictionary<Char, Char>() {
    {'[', '('},
    {']', ')'},
    {'{', '-'},
  };

then you can use Linq:

  string strValue = "MyString[]{"; 

  String result = new String(strValue
    .Select(c => { 
       Char substitute;

       if (replaces.TryGetValue(c, out substitute))
         return substitute;
       else 
         return c;  
       })
    .ToArray());

As an alternative, you can put a foreach loop:

  StringBuilder sb = new StringBuilder(strValue.Length);

  foreach (var c in strValue) {
    Char substitute;

    if (replaces.TryGetValue(c, out substitute))
      sb.append(substitute);  
    else 
      sb.append(c);   
  }

  String result = sb.ToString();
Sign up to request clarification or add additional context in comments.

Comments

1

Linq approach

string strValue = "MyString[]{"; 
char[] findArray = {'[',']','{'};
char[] replaceArray = { '(', ')', '-' };

Func<char, int, char> GetC = (x, c) => c > -1 ? replaceArray.ElementAt(c) : x;
string Result = string.Concat(strValue.Select(x => GetC(x, Array.IndexOf(findArray, x))));

1 Comment

every answer seems working fine ...and thank you all guys
1

This is the optimal way. No need to use LINQ here.

string strValue = "MyString[]{";
char[] findArray = { '[', ']', '{' };
char[] replaceArray = { '(', ')', '-' };

char[] charsValue = strValue.ToCharArray();

for (int i = 0; i < charsValue.Length; i++)
{
    for (int j = 0; j < findArray.Length; j++)
    {
        if (charsValue[i] == findArray[j])
        {
            charsValue[i] = replaceArray[j];
            continue;
        }
    }
}

string newStrValue = new string(charsValue); // MyString()-

3 Comments

Are you sure that O(m*n) where m=charsValue.Length; n=findArray.Length is a good way to solve this?
@SerhiyChupryk what makes you think there's a better way?
This problem should have (intuition) linear solution like the one proposed by Dmitry Bychenko
1

Here's a one-line solution:

string strValue = "MyString[]{";
char[] findArray = { '[', ']', '{' };
char[] replaceArray = { '(', ')', '-' };

string newStr = String.Join("", strValue.Select(c => findArray.Contains(c) ? replaceArray[Array.IndexOf(findArray, c)] : c));

Console.WriteLine(newStr); // MyString()-

Comments

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.