Programming

변수 안에있는 문자열에 큰 따옴표를 추가하는 방법은 무엇입니까?

procodes 2020. 6. 9. 22:44
반응형

변수 안에있는 문자열에 큰 따옴표를 추가하는 방법은 무엇입니까?


나는 다음과 같은 변수를 가지고있다 :

string title = string.empty;

내 필요는 문자열이 전달되면 div 안에 내용을 큰 따옴표로 표시해야한다는 것입니다. 그래서 나는 다음과 같이 썼다 :

...
...
<div>"+ title +@"</div>
...
...

그러나 여기에 큰 따옴표를 추가하는 방법은 무엇입니까? 다음과 같이 표시됩니다.

"How to add double quotes"

두 배로 늘려서 탈출해야합니다 (verbatim 문자열 리터럴).

string str = @"""How to add doublequotes""";

또는 일반 문자열 리터럴을 사용하면 \:

string str = "\"How to add doublequotes\"";

따라서 본질적으로 문자열 변수 내에 큰 따옴표를 저장하는 방법을 묻고 있습니까? 이를위한 두 가지 솔루션 :

var string1 = @"""inside quotes""";
var string2 = "\"inside quotes\"";

어쩌면 좀 더 명확하게하기 위해 :

var string1 = @"before ""inside"" after";
var string2 = "before \"inside\" after";

귀하의 질문을 올바르게 이해하면 다음을 시도해보십시오.

string title = string.Format("<div>\"{0}\"</div>", "some text");

이 작업을 자주 수행해야하고 코드에서 더 깨끗하게하려면 확장 방법을 사용하는 것이 좋습니다.

이것은 정말 명백한 코드이지만 여전히 시간을 절약하고 절약하는 것이 유용 할 수 있다고 생각합니다.

  /// <summary>
    /// Put a string between double quotes.
    /// </summary>
    /// <param name="value">Value to be put between double quotes ex: foo</param>
    /// <returns>double quoted string ex: "foo"</returns>
    public static string AddDoubleQuotes(this string value)
    {
        return "\"" + value + "\"";
    }

그런 다음 원하는 모든 문자열에서 foo.AddDoubleQuotes () 또는 "foo".AddDoubleQuotes ()를 호출 할 수 있습니다.

이 도움을 바랍니다.


다른 참고 사항 :

    string path = @"H:\\MOVIES\\Battel SHIP\\done-battleship-cd1.avi"; 
    string hh = string.Format("\"{0}\"", path);
    Process.Start(@"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe ", hh + " ,--play");

hh의 실제 값은 "H : \ MOVIES \ Battel SHIP \ done-battleship-cd1.avi"입니다.

이중 이중 리터럴이 필요한 경우 다음을 사용하십시오. @ "H : \ MOVIES \ Battel SHIP \ done-battleship-cd1.avi"; 대신 @@ H : \ MOVIESBattel SHIP \ done-battleship-cd1.avi "; 첫 번째 리터럴은 경로 이름을위한 것이고 두 번째 리터럴은 큰 따옴표를위한 것이므로


큰 따옴표를 작은 따옴표로 포함 할 수도 있습니다.

string str = '"' + "How to add doublequotes" + '"';

&quot;대신에 사용할 수 있습니다 ". 브라우저에서 올바르게 표시됩니다.


작업 예제 와 함께 문자열 보간 사용 :

var title = "Title between quotes";
var string1 = $@"<div>""{title}""</div>";  //Note the order of the $@
Console.WriteLine (string1);

산출

<div>"Title between quotes"</div>

사용할 수 있습니다 $.

보간 문자열 : 문자열을 구성하는 데 사용됩니다. 보간 된 문자열은 보간 된 표현식을 포함하는 템플릿 문자열처럼 보입니다. 보간 된 문자열은 포함 된 보간 된 표현식을 해당 문자열 표현으로 대체하는 문자열을 반환합니다. 이 기능은 C # 6 이상 버전에서 사용할 수 있습니다.

string commentor = $"<span class=\"fa fa-plus\" aria-hidden=\"true\"> {variable}</span>";

둘 중 하나를 사용하십시오

& dquo;
<div> & dquo; "+ 제목 + @"& dquo; </ div>

또는 큰 따옴표를 피하십시오 :

\ "
<div> \ ""+ 제목 + @ "\"</ div>

string doubleQuotedPath = string.Format(@"""{0}""",path);

큰 따옴표 앞에 백 슬래시 (\)를 넣으십시오. 작동합니다.


C #에서는 다음을 사용할 수 있습니다.

 string1 = @"Your ""Text"" Here";
 string2 = "Your \"Text\" Here";

in C# if we use "\" means that will indicate following symbol is not c# inbuild symbol that will use by developer. so in string we need double quotes means we can put "\" symbol before double quotes. string s = "\"Hi\""


"<i class=\"fa fa-check-circle\"></i>" is used with ternary operator with Eval() data binding:

Text = '<%# Eval("bqtStatus").ToString()=="Verified" ? 
       Convert.ToString("<i class=\"fa fa-check-circle\"></i>") : 
       Convert.ToString("<i class=\"fa fa-info-circle\"></i>"

An indirect, but simple to understand alternative to add quotes at start and end of string -

char quote = '"';

string modifiedString = quote + "Original String" + quote;

If you want to add double quotes in HTML

echo "<p>Congratulations,  &#8220; ". $variable ." &#8221;!</p>";
output -> Congratulations, "Mr Jonh "!

If you want to add double quotes to a string that contains dynamic values also. For the same in place of CodeId[i] and CodeName[i] you can put your dynamic values.

data = "Test ID=" + "\"" + CodeId[i] + "\"" + " Name=" + "\"" + CodeName[i] + "\"" + " Type=\"Test\";

참고URL : https://stackoverflow.com/questions/3905946/how-to-add-double-quotes-to-a-string-that-is-inside-a-variable

반응형