development

.NET의 문자열에서 큰 따옴표 제거

big-blog 2020. 9. 17. 08:23
반응형

.NET의 문자열에서 큰 따옴표 제거


일관되지 않은 형식의 HTML과 일치 시키려고하는데 큰 따옴표를 제거해야합니다.

흐름:

<input type="hidden">

목표:

<input type=hidden>

제대로 이스케이프하지 않았기 때문에 이것은 잘못된 것입니다.

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

빈 문자가 없기 때문에 이것은 잘못된 것입니다.

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

큰 따옴표를 빈 문자열로 대체하기위한 구문 / 이스케이프 문자 조합은 무엇입니까?


첫 번째 줄이 실제로 작동한다고 생각하지만 단일 줄을 포함하는 문자열에 대해 4 개의 따옴표가 필요하다고 생각합니다 (최소한 VB에서).

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

C #의 경우 백 슬래시를 사용하여 따옴표를 이스케이프해야합니다.

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

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

문자열에서 큰 따옴표 문자를 이스케이프하려면 \를 사용해야합니다.


나는 내 생각이 이미 반복되는 것을 보지 못했기 때문에 string.TrimC #에 대한 Microsoft 문서에서 단순히 빈 공간을 자르는 대신자를 문자를 추가 할 수 있음을 살펴볼 것을 제안 합니다.

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

withOutQuotes가 "hello"대신""hello""


다음 중 하나를 사용할 수 있습니다.

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

...하지만 왜 그렇게하려고하는지 궁금 해요? 인용 된 속성 값을 유지하는 것이 좋은 습관이라고 생각 했습니까?


s = s.Replace("\"",string.Empty);

c # : "\""이므로s.Replace("\"", "")

vb / vbs / vb.net : ""따라서s.Replace("""", "")


백 슬래시로 큰 따옴표를 이스케이프해야합니다.

s = 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;

If you only want to strip the quotes from the ends of the string (not the middle), and there is a chance that there can be spaces at either end of the string (i.e. parsing a CSV format file where there is a space after the commas), then you need to call the Trim function twice...for example:

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( """", "" )

Two quotes next to each other will function as the intended " character when inside a string.

참고URL : https://stackoverflow.com/questions/1177872/strip-double-quotes-from-a-string-in-net

반응형