Reputation: 1468
I want to remove quote from starting of the string and end of the string. But my existing code is removing all quotes from the string. I tried to replace with Trim()
method. But no hopes.
My code here
result = value.Replace("'", "").Split(',').ToList();
I tried the following also,
result = value.TrimStart(Convert.ToChar("'"))
.TrimEnd(Convert.ToChar("'"))
.Split(',')
.ToList();
Please give me the proper way to do this.
Upvotes: 10
Views: 32853
Reputation: 141
if (value.Length >= 2 && value[0] == '\'' && value[value.Length - 1] == '\'') { value = value.Substring(1, value.Length - 2); }
this will check if the first and last char are actually single quote and if so remove it
Upvotes: 0
Reputation: 29036
You can try .Trim()
like the following:
string inputStr = "'some string'";
string outputStr = inputStr.Trim(new char[]{(char)39});
Where (char)39
represents '
, and the .Trim()
will remove the first and last '
from the string; You can try like this as well:
string outputStr = inputStr.Trim('\'');
You can take a look into this Example
Upvotes: 22
Reputation: 518
string inputStr = "'some string'";
string outputStr = inputStr.Trim('\'')
Upvotes: 3
Reputation: 2163
try this
int indexOfFirst = value.IndexOf('\'');
string temp = value.Remove(indexOfFirst, 1);
int indexOfLast = temp.LastIndexOf('\'');
temp = temp.Remove(indexOfLast, 1);
Hope this was useful.
Upvotes: 1