user710502
user710502

Reputation: 11469

Eliminating characters from string

I am reading from a file, and some of the data is comming in like this

"\"ZIP\""

so when i try to assign it its causing errors, i want to get rid off the extra \", so if I assign it to as string like

string s = data[1].ToString();

what s is "\"ZIP\""

i just want it to be "ZIP", i tried:

string s = data[1].ToString().replace("\\\"","");

but no luck. Any help would be much appreciated.

Upvotes: 0

Views: 103

Answers (4)

Adil
Adil

Reputation: 148180

Remove the escape characters in the string by split and create new string. You can include as many characters in escape sequence array as you want.

StringBuilder sb = new StringBuilder();
string[] parts = inputString.Split(new char[] {'"'};
 StringSplitOptions.RemoveEmptyEntries);
int size = parts.Length;
for (int i = 0; i < size; i++)
    sb.AppendFormat("{0} ", parts[i]);= 

string strWithoutEscape = sb.ToString();

Upvotes: 1

user1333371
user1333371

Reputation: 618

Try:

data[1].toString().replace("\\\"", "");

Notice that the function is case-sensitive, so using ToString would fail.

Upvotes: 0

Steve
Steve

Reputation: 216352

String.Trim could be used with an array of char to remove from start and end of a string

char[] charsToTrim = { '"', '\\'};
string s = data[1].ToString().Trim(charsToTrim);

Upvotes: 1

cuongle
cuongle

Reputation: 75326

just try:

var result = "\"ZIP\"".Replace("\"", "");

Or:

 var result = "\"ZIP\"".Trim('"');

Upvotes: 2

Related Questions