0

For example I have the following code:

 string Sheet[] = new string[]{

  "ABCDEFG",
  "1234567",
  "abcdefg"  };

while(true){

Console.SetCursorPosition(0, 0);
Console.WriteLine(string.Join(Environment.NewLine, Sheet));  }

However, I want to replace 'C' with '#'.

Because the cycle constantly reprints the text sheet, setting the cursor position and overwriting the 'C' doesn't solve my problem. I thought maybe if I used the

 string.replace(Oldchar, NewChar)

function it may work. My question is: How can I select the specific string from the array and then the specific char?

EDIT: I may have asked the question a bit too vaguely. What if there are more 'C' characters in that string, but I want to repalce the 'C' in that specific position? It doesn't have to be it's first occurance.

Hexanix
  • 67
  • 1
  • 6
  • Iterate through Array, check if it **Contains** character `C` and then use `String.Replace`, or simply call `String.Replace` without searching for character. Remember to assign the result of `String.Replace` back to your element. – Habib Aug 04 '16 at 15:51
  • For each ( string tmp in sheet){ tmp.replace(old,new) ;} – icbytes Aug 04 '16 at 15:52
  • What if I had multiple 'C' characters in that string? – Hexanix Aug 04 '16 at 15:53
  • @Hexanix `Replace` works for multiple occurences too. Note that the code examples in these comments so far all don't work, because strings are immutable and the loops shown above don't _change the strings in your array_, but only create new ones that are thrown away immediatly. – René Vogt Aug 04 '16 at 15:57
  • See [Replace first occurrence of pattern in a string](http://stackoverflow.com/questions/8809354/replace-first-occurrence-of-pattern-in-a-string) and [Replace all occurences of a string from a string array](http://stackoverflow.com/questions/11789750/replace-all-occurences-of-a-string-from-a-string-array). – CodeCaster Aug 04 '16 at 16:12
  • @CodeCaster I'm asking for a way to replace a character in a specific position, not neccesarily the it's first occurance in the string – Hexanix Aug 04 '16 at 16:15
  • Yeah so if the Nth character is X, then set the Nth character to Y? See http://stackoverflow.com/questions/3306568/how-do-i-set-a-character-at-an-index-in-a-string-in-c instead then. – CodeCaster Aug 04 '16 at 16:22

2 Answers2

3

Strings are immutable in C#. This means you cannot change a string, but only create a new one from the old with specific changes.

For your case this means you have to replace all the strings in your array with new ones created by replacing C with # in the old ones

for(int i=0; i<Sheet.Length; i++) Sheet[i] = Sheet[i].Replace("C", "#");

or if you can replace the whole array:

Sheet = Sheet.Select(s => s.Replace("C", "#")).ToArray();

UPDATE: if you want to replace only a single character in a string, let's say the first occurence of 'C', you can do this:

for(int i=0; i<Sheet.Length; i++)
{
    string old = Sheet[i];
    int index = old.IndexOf('C');
    if (index < 0) continue; // no C in this string
    Sheet[i] = old.Remove(index, 1).Insert(index, "#");
}

So you remove the 1 characer and insert the new one.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • What if there are more 'C' characters in that string, but I want to replace the 'C' in that specific position? – Hexanix Aug 04 '16 at 15:58
  • @Hexanix what specific position would that be? – René Vogt Aug 04 '16 at 15:59
  • I'd upvote you back up, but I just created the account :') – Hexanix Aug 04 '16 at 16:00
  • well for example if I had a string array "ABCCECG", "1234567", "abcdefg" And I want to replace the C in the string's 3rd position (before the E) how would I do that without replacing the rest of the 'C' chars? – Hexanix Aug 04 '16 at 16:02
  • @Hexanix updated the answer – René Vogt Aug 04 '16 at 16:03
  • @Hexanix updated again with a simpler way. – René Vogt Aug 04 '16 at 16:07
  • won't this work only if I want to replace the first occurance óf 'C'? – Hexanix Aug 04 '16 at 16:11
  • 1
    @Hexanix I can't give you an algorithm if you don't specify criterias. In the _special_ case you mentioned, just replace `Sheet[0] = Sheet[0].Remove(3,1).Insert(3, "#")` or if you simply know the expected result, just set `Sheet[0] = "ABC#ECG";` How should my algorithm know that you want the second `C`? Or do you mean you _always_ want the second one? Or the second last one? Or the one in the middle? I don't know your requirements, so I can't help you. – René Vogt Aug 04 '16 at 16:16
  • I think I figured out a way to implement it correctly. I'll look into it when once I get some rest. Thank you for your help! ^^ – Hexanix Aug 04 '16 at 16:33
0

I think the easiest way to do this is by using Linq.

Include LINQ by adding using System.LINQ to the header of your file.

Then you can replace your last line with

Console.WriteLine( string.Join(Environment.NewLine, Sheet.Select(s => s.Replace('C', '#')).ToArray() ) );

This will replace the character 'C' with '#' in the whole array.

ArneJ
  • 1
  • 1