Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
example:
String str = "1 2 3 4 5 6 7 8 9";
Console.WriteLine("Original string: \"{0}\"", str);
Console.WriteLine("CSV string:      \"{0}\"", str.Replace(' ', ','));


if I want to substitute not all empty spaces but only the second and the third, how must I write?

What I have tried:

I tried to use the code that I wrote above
Posted
Updated 13-Apr-23 2:22am
Comments
PIEBALDconsult 12-Apr-23 11:49am    
Use a Regular Expression.

If you are familiar with Linq you can try something along these lines

.
C#
string str = "1 2 3 4 5 6 7 8 9";
  int[] spacesToReplace = new int[] { 2, 3 };//replace only the 2nd and third space
  //select the index value of each space in the string
  var spaceIndices = str.Select((c, i) => c == ' ' ? i : -1).Where((i) => i != -1).ToArray();
  //select the spaces that need to be replaced
  var targetedSpaces = spacesToReplace.Select((n) => spaceIndices[n - 1]);
  //replace the selected spaces with a ','
  var charArray = str.Select((c, i) => targetedSpaces.Contains(i) ? ',' : c).ToArray();
  //build a new string the easy way
  string updatedString = new(charArray);

  Console.WriteLine($"Updated string: {updatedString}");
  //prints Updated string: 1 2,3,4 5 6 7 8 9
 
Share this answer
 
The Replace method is used to replace all occurrences. To restrict it to specific positions you probably need something like the String.Chars[Int32] Property (System) | Microsoft Learn[^].
 
Share this answer
 
Comments
Member 14594285 12-Apr-23 5:12am    
yes but this is for a char..but if I must substitute a substring how must I write?
Richard MacCutchan 12-Apr-23 5:29am    
You can still replace more than one character using a simple loop. Alternatively you could use the Substring method to separate the two parts. You can then do the replacement for the part you need, and recombine them to get the final string.
Try a regex:
RegEx
(?<=^\d+\s\d+)(\s)(?<digit>\d+)(\s)(?=.*)
With a replacement string of
RegEx
${digit}
 
Share this answer
 
Assuming I have understood the required result, I tried a C# approach.

String str = "1 2 3 4 5 6 7 8 9";

Console.WriteLine("Original string: \"{0}\"", str);
Console.WriteLine("CSV string:      \"{0}\"", str.Replace(' ', ','));

var chars = new List<char>();
chars.AddRange(str);
chars.RemoveAt(3);
chars.RemoveAt(4);
var result = String.Join("", chars);

Console.WriteLine("New CSV string:      \"{0}\"", result.Replace(' ', ','));
Console.ReadKey();


This converts the string to a list, removes the spaces at position 3 & 5, and converts the list back to a string.

The output:
New CSV string:      "1,234,5,6,7,8,9"
 
Share this answer
 
v2
You can separate the contents by using split (this will remove the double spaces)

string[] values=str.Split();
or
string[] values=str.Split(' ');

the second sptep is join it again with

string newSTR=string.Join(",", values);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900