web-dev-qa-db-ja.com

.NETの文字列から二重引用符を取り除く

一貫性のない形式のHTMLで一致させようとしていますが、二重引用符を削除する必要があります。

現在:

<input type="hidden">

目標:

<input type=hidden>

私はそれを適切にエスケープしていないので、これは間違っています:

s = s.Replace( "" "、" ");

(私の知る限りでは)空白文字が存在しないため、これは間違っています。

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

二重引用符を空の文字列に置き換えるための構文/エスケープ文字の組み合わせとは何ですか?

83
Even Mien

最初の行は実際に機能すると思いますが、単一の文字列を含む文字列には4つの引用符が必要だと思います(少なくともVB内):

s = s.Replace("""", "")

c#の場合、バックスラッシュを使用して引用符をエスケープする必要があります。

s = s.Replace("\"", "");
185
Joey
s = s.Replace("\"", "");

文字列内の二重引用符をエスケープするには、\を使用する必要があります。

25
David

私の考えはまだ繰り返されていませんでしたので、C#のMicrosoftドキュメントのstring.Trimを見て、空のスペースを単にトリミングする代わりに、トリミングする文字を追加することをお勧めします。

string withQuotes = "\"hellow\"";
string withOutQotes = withQuotes.Trim('"');

withOutQuotesは"hello"ではなく""hello""になります

15
Shane

次のいずれかを使用できます。

s = s.Replace(@"""","");
s = s.Replace("\"","");

...しかし、なぜあなたはそれをしたいのか興味がありますか?属性値を引用しておくのは良い習慣だと思いましたか?

14
Fredrik Mörk
s = s.Replace("\"",string.Empty);
6
Steve Gilham

c#:"\""、したがってs.Replace("\"", "")

vb/vbs/vb.net:""したがってs.Replace("""", "")

5
svinto

二重引用符をバックスラッシュでエスケープする必要があります。

s = s.Replace("\"","");
3
Jake Pearson

s = s.Replace(@ "" ""、 "");

1
gHeidenreich

これは私のために働いた

//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;
1
user3519062

文字列の両端(中央ではなく)から引用符を削除するだけで、文字列の両端にスペースが存在する可能性がある場合(つまり、CSV形式のファイルを解析して、カンマ)、Trim関数を呼び出す必要がありますtwice ...たとえば:

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)
1
TJC
s = s.Replace( """", "" )

隣り合う2つの引用符は、文字列内にある場合に、意図した「文字」として機能します。

0
Mike