programing

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

elecom 2023. 7. 2. 19:02
반응형

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

형식이 일치하지 않는 HTML과 일치하려고 하는데 이중 따옴표를 제거해야 합니다.

현재:

<input type="hidden">

목표:

<input type=hidden>

내가 제대로 탈출하지 못했기 때문에 이것은 잘못된 것입니다.

s = s.교체("","");

공백 문자가 없기 때문에 잘못된 것입니다(제가 알기로는).

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

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

첫 번째 줄이 실제로 작동할 것이라고 생각하지만 하나의 줄(VB 단위 이상)을 포함하는 문자열에는 따옴표가 4개 필요합니다.

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

C#의 경우 백슬래시를 사용하여 따옴표에서 벗어나야 합니다.

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

제 생각이 반복되는 걸 아직 못 봤으니까 여러분들이 한번 봐주셨으면 좋겠습니다.string.TrimC#에 대한 Microsoft 설명서에서는 빈 공간을 단순히 트리밍하는 대신 트리밍할 문자를 추가할 수 있습니다.

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

결과적으로 OutQuotes가 없어야 합니다."hello"대신에""hello""

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

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

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

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

그런데 왜 그러시는지 궁금하군요속성 값을 따옴표로 묶는 것이 좋은 관행이라고 생각했습니다.

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

c#:"\"",따라서s.Replace("\"", "")

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

문자열의 끝 부분(중간 부분이 아님)에서만 따옴표를 제거하고 문자열의 양쪽 끝에 공백이 있을 가능성이 있는 경우(예: 쉼표 뒤에 공백이 있는 CSV 형식 파일 구문 분석) 트림 기능을 두 번 호출해야 합니다.예:

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

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

두 따옴표는 문자열 내에서 서로 옆에 있으면 의도된 "문자"로 작동합니다.

단일 문자를 제거하려면 배열을 읽고 해당 문자를 건너뛰고 배열을 반환하는 것이 더 쉽습니다.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('"');

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

반응형