Nadav
Nadav

Reputation: 2717

removing ' delimiters from a string

hello i am trying to split a string using the split method of string

 string toSplit = "hello how 'are u"
 string[] arr = toSplit.Split('''); // that code doesnt compile 
 for (int i=0 ; i < arr.Length ; i++)
      Console.write("arr[i]="+ arr[i]);

my output is:

arr[0] = hello 
arr[1]=how, // i get this output by removing the split ofc , it doesnt compile 
arr[2]='are 
and arr[3]=u

and what i want is to remove this ' delimiter from arr[2]

thanks in advance for you kind help.

Upvotes: 1

Views: 7002

Answers (2)

Baz1nga
Baz1nga

Reputation: 15579

string toSplit = "hello how 'are u";
string[] arr = toSplit.Split('\'');
var arr=arr.Split(' ');
 for (int i=0 ; i < arr.Length ; i++)
      Console.Write("arr[i]="+ arr[i]);

Mistakes in code:

  1. No semi colon in the string declaration line
  2. You were not escaping ' Use escape character \
  3. Console.Write and not write

Upvotes: 3

Adam
Adam

Reputation: 16199

You can just do

toSplit = toSplit.Replace("'", "");

before you split

But I am not quite understanding your question. Your title says you want to remove ' from a string.

I am also unsure how your code gets 4 objects in an array by splitting by ' since there is only one in your string.

The array would look like that if you did a split with a space character.

So do this to get the output you want:

string toSplit = "hello how 'are u";
toSplit = toSplit.Replace("'", ""); 
string[] arr = toSplit.Split(' '); 
for (int i=0 ; i < arr.Length ; i++) 
    Console.Write("arr[i]="+ arr[i]); 

Upvotes: 3

Related Questions