There are three lines in which there are rows of the same length, that is, 3 bits in each of the lines, and again 3 bits through the space and so 10 times, as the example showed 2 columns and 10 such pieces

xxx xxx xxx xxx xxx xxx 

to make it clearer how I can do in the first column of .replace ("x", "new letter"); and do the same thing with the second column of the first letter in all the lines of the first character karoche? And save the changes there as well

  • one
    And why work with inconvenient strings, and not deserialize the file into the object structure, change, and serialize again? Everything will become much easier. - VladD

2 answers 2

You can like this:

 var txt = "xxx xxx\r\nxxx xxx\r\nxxx xxx"; var lines = txt.Split(new string[] { "\r\n" }, StringSplitOptions.None); // заменить первую for (int i = 0; i < lines.Length; i++) { lines[i] = lines[i].Remove(0, 1).Insert(0, "y"); } // заменить первую в следующем слове for (int i = 0; i < lines.Length; i++) { lines[i] = lines[i].Remove(4, 1).Insert(4, "y"); } var result = string.Join("\r\n", lines); 

The principle is simple. Split the file in rows, and then take the first line and look for the desired position of the desired character. Then, sorting through all the lines, delete the old character and insert a new one for this position.

Here is another example of replacing the first character in each column of each row:

 var txt = "xxx xxx xxx\r\nxxx xxx xxx\r\nxxx xxx xxx"; var lines = txt.Split(new string[] { "\r\n" }, StringSplitOptions.None); for (int i = 0; i < lines.Length; i++) { var columns = lines[i].Split(' '); for (int j = 0; j < columns.Length; j++) { columns[j] = columns[j].Remove(0, 1).Insert(0, "y"); } lines[i] = string.Join(" ", columns); } var result = string.Join("\r\n", lines); 
  • And what, in c # you can't do something like text[n] = 'Z' ? - Alekcvp
  • in c # string objects are immutable, you can read the index, but you can not change it. You can get an array of characters from a string, do the necessary manipulations and then create a new string. var chars = columns [j] .ToCharArray (); chars [0] = 'y'; columns [j] = new string (chars); - Eugene
  • I wonder how it affects performance when to replace a single character you need to do Remove / Insert (that is, in fact, copy the entire line twice)? .. - Alekcvp
  • c # works very slyly with strings and therefore needs to be profiled. If it is a lot of operations with strings, it is better to use StringBuilder and read from the source string immediately change and write to StringBuilder. Under the hood, he creates a buffer and, if necessary, expands it. - Eugene

If the question is how to replace the first character in each string, use the Substring method in the loop iteration cycle.

Sample code does not make sense to lay out, you have to learn how to use this simple method.

Update:

  private void Check() { var result = ""; var ch = 'a'; var list = new List<string>() { "xxx xxx", "xxx xxx", "xxx xxx" }; foreach (var line in list) result += ReplaceFirstChar(line, ch) + "\r\n"; MessageBox.Show(result); } private string ReplaceFirstChar(string str, char ch) { var result = ""; foreach (var line in str.Split(' ')) result += ch + line.Substring(1, line.Length - 1) + ' '; return result.Trim(); } 

check.

  • Yes, I know how to write code, the problem is not this, how can I replace the first character in the second column? - komra23
  • @May_be, loop through all the lines, find the desired line, change the value. What is the actual problem? What exactly does not work? - Align
  • @May_be, "how do I replace the first character in the second column" - what is the problem to find the first occurrence of "_x" in the line and replace it with "_y" (_ is a space)? Moreover, if you have a hard-coded format, then you can just simply replace the 5th character in each line with the desired one. - Alekcvp pm
  • @May_be, updated the answer, check. - Align
  • @Alekcvp I did it, I just registered the type .... = ..... Insert (3, ""); - komra23 pm