回答:
s = s.Replace("\"", "");
文字列内の二重引用符をエスケープするには、\を使用する必要があります。
次のいずれかを使用できます。
s = s.Replace(@"""","");
s = s.Replace("\"","");
...しかし、なぜあなたがそれをしたいのか、私は気になりますか?属性値を引用符で囲むことは良い習慣だと思いましたか?
c#"\"":、したがってs.Replace("\"", "")
vb / vbs / vb.net:""したがってs.Replace("""", "")
二重引用符はバックスラッシュでエスケープする必要があります。
s = s.Replace("\"","");
これは私のために働いた
//Sentence has quotes
string nameSentence = "Take my name \"Wesley\" out of quotes";
//Get the index before the quotes`enter code here`
int begin = nameSentence.LastIndexOf("name") + "name".Length;
//Get the index after the quotes
int end = nameSentence.LastIndexOf("out");
//Get the part of the string with its quotes
string name = nameSentence.Substring(begin, end - begin);
//Remove its quotes
string newName = name.Replace("\"", "");
//Replace new name (without quotes) within original sentence
string updatedNameSentence = nameSentence.Replace(name, newName);
//Returns "Take my name Wesley out of quotes"
return updatedNameSentence;
文字列の末尾(中央ではなく)から引用符を取り除きたい場合、文字列の両端にスペースがある可能性があります(つまり、後にスペースがあるCSV形式のファイルを解析します)コンマ)、次にTrim関数を2回呼び出す必要があります...例:
string myStr = " \"sometext\""; //(notice the leading space)
myStr = myStr.Trim('"'); //(would leave the first quote: "sometext)
myStr = myStr.Trim().Trim('"'); //(would get what you want: sometext)
s = s.Replace( """", "" )
隣り合った2つの引用符は、文字列内で「意図した」文字として機能します。
単一の文字を削除したい場合は、配列を読み取ってその文字をスキップして配列を返すほうが簡単だと思います。vcardのjsonをカスタム解析するときに使用します。「引用符で囲まれた」テキスト識別子を持つ悪いjsonなので。
以下のメソッドを拡張メソッドを含むクラスに追加します。
public static string Remove(this string text, char character)
{
var sb = new StringBuilder();
foreach (char c in text)
{
if (c != character)
sb.Append(c);
}
return sb.ToString();
}
その後、この拡張メソッドを使用できます。
var text= myString.Remove('"');