0

How can I remove empty space between " " ?. I have more than 100 lines of rich text box and my sentence goes like below with empty space between " ".

my sentence

remove only " railing" spaces of a string in Java
remove only " trailing" spaces of a string in Java
remove only " ling" spaces of a string in Java
remove only " ing" spaces of a string in Java
.
.
.

should be:

remove only "railing" spaces of a string in Java
remove only "trailing" spaces of a string in Java
remove only "ling" spaces of a string in Java
remove only "ing" spaces of a string in Java
.
.
.

My code

richTextBox1.lines.Trim().Replace("\"  \" ", " ");
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
user2760129
  • 161
  • 1
  • 10

3 Answers3

1

Using regex:

string RemoveBetween(string s, char begin, char end)
{
    Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", begin, end));
    return regex.Replace(s, string.Empty);
}

string s = "remove only \"railing\" spaces of a string in Java";
s = RemoveBetween(s, '"', '"');

source: https://stackoverflow.com/a/1359521/1714342

You can define between which characters you wish to remove string. Read more about Regex.Replace

EDIT:

missunderstood, you are just missing assign in richTextBox1.lines.Trim().Replace("\" \" ", " ");

make it:

richTextBox1.lines = richTextBox1.lines.Trim().Replace("\"  \" ", " ");

Replace is not changing string.

Community
  • 1
  • 1
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • my sentence may change but only error is space that between " " so how can I make your code generic. not only string s – user2760129 Oct 03 '13 at 07:03
  • got this error: 'System.Array' does not contain a definition for 'Trim' and no extension method 'Trim' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) – user2760129 Oct 03 '13 at 07:16
0

You're missing the reassign to richTextBox1. Replace() returns string value with correct text. Your code should be:

for(int i = 0; i < richTextBox1.Lines.Count(); i++)
{
    richTextBox1.Lines[i] = richTextBox1.Lines[i].Trim().Replace("\" \" ", " ");
}
borkovski
  • 938
  • 8
  • 12
0

Try this:

richTextBox1 = richTextBox1.lines.Trim().Replace(" \" ", " \"");
jayt.dev
  • 975
  • 6
  • 14
  • 36