2011-04-21 15 views
14

是否有一些在c#中的字符串周圍添加引號的方法?c#中的字符串自動引號 - 內置方法?

+1

你能否澄清一下你的問題! – lKashef 2011-04-21 15:09:58

+0

from string abc我想讓字符串「abc」,我想知道是否有一些可以完成這個工作的類中的方法。我知道它是5行代碼來編寫你自己的。 – Darqer 2011-04-21 15:12:28

+0

@ Darger:如果字符串包含引號會怎麼樣?你會用它做什麼? – 2011-04-21 19:52:00

回答

24

你的意思是只是加引號?喜歡這個?

text = "\"" + text + "\""; 

?我不知道一個內置的方法來做到這一點的,但如果你想它會很容易寫一個:

public static string SurroundWithDoubleQuotes(this string text) 
{ 
    return SurroundWith(text, "\""); 
} 

public static string SurroundWith(this string text, string ends) 
{ 
    return ends + text + ends; 
} 

這樣,它是一個小更普遍的:

text = text.SurroundWithDoubleQuotes(); 

text = text.SurroundWith("'"); // For single quotes 

我不能說我需要做的這往往足以令它值得擁有雖然方法...

+0

是的,但我不知道 – Darqer 2011-04-21 15:10:53

+0

裏面是否有東西在打滑。 '這個文本'應該是'這個字符串文本':) – Jamiec 2011-04-21 15:14:33

+1

@Jamiec:Humbug :)固定... – 2011-04-21 15:25:48

7

燁s表示,採用級聯和轉義字符

myString = "\"" + myString + "\""; 

也許擴展方法

public static string Quoted(this string str) 
{ 
    return "\"" + str + "\""; 
} 

用法:

var s = "Hello World" 
Console.WriteLine(s.Quoted()) 
+0

字符不需要轉義:'''+ myString +''' – Jerther 2015-04-16 13:14:17

+0

@另外 - 你究竟在說什麼?你是否真的閱讀過這個問題和字面上的每一個答案? – Jamiec 2015-04-16 13:33:14

+1

我在說使用字符而不是字符串,是的,我讀過其他答案。 – Jerther 2015-04-16 14:14:33

3

沒有,但你可以寫你自己的,或者創建一個擴展方法

string AddQuotes(string str) 
{ 
    return string.Format("\"{0}\"", str); 
} 
+0

擴展方法看起來不錯,謝謝。 – Darqer 2011-04-21 15:14:40

9
string quotedString = string.Format("\"{0}\"", originalString); 
2

使用轉義字符

只是前面加上一個反斜槓,這被稱爲一個escape character特殊字符。

簡單的例子

string MyString = "Hello"; 
Response.Write(MyString); 

這將打印:

Hello 

但是:

string MyString = "The man said \"Hello\""; 
Response.Write(MyString); 

將打印:

The man said "Hello" 

替代

可以使用有用@運營商,以幫助逃避字符串,看到這個鏈接:

http://www.kowitz.net/archive/2007/03/06/the-c-string-literal

然後,行情,你會用雙引號表示一個單引用。例如:

string MyString = @"The man said ""Hello"" and went on his way"; 
Response.Write(MyString); 

輸出:

The man said "Hello" and went on his way 
1

沒有這樣的內置的方法做你的要求

有SplitQuotes方法做一些事情 輸入 - 這是一個「很長「字符串 輸出 - 這是一個非常長的字符串

當您從文本框或某個控件得到一個字符串時,它帶有」 ES。

如果仍然要放置的報價,那麼你可以使用這種方法

private string PlaceQuotes(string str, int startPosition, int lastPosition) 
     { 
      string quotedString = string.Empty; 
      string replacedString = str.Replace(str.Substring(0, startPosition),str.Substring(0, startPosition).Insert(startPosition, "'")).Substring(0, lastPosition).Insert(lastPosition, "'"); 
      return String.Concat(replacedString, str.Remove(0, replacedString.Length)); 
     } 
1

我是新手我自己的一點C#,所以纔有我,但我有這樣一個catch-所有實用工具類,因爲我錯過的Perl:

// overloaded quote - if no quote chars spec'd, use "" 
public static string quote(string s) { 
    return quote(s, "\"\""); 
} 

// quote a string 
// q = two quote chars, like "", '', [],(), {} ... 
//  or another quoted string (quote-me-like-that) 
public static string quote(string s, string q) { 

    if(q.Length == 0)  // no quote chars, use "" 
     q = "\"\""; 
    else if(q.Length == 1) // one quote char, double it - your mileage may vary 
     q = q + q; 
    else if(q.Length > 2) // longer string == quote-me-like-that 
     q = q.Substring(0, 1) + q.Substring(q.Length - 1, 1); 

    if(s.Length == 0) // nothing to quote, return empty quotes 
     return q; 

    return q[0] + s + q[1]; 

} 

使用方法如下:

quote("this with default"); 
quote("not recommended to use one char", "/"); 
quote("in square brackets", "[]"); 
quote("quote me like that", "{like this?}"); 

返回:

"this with default" 
/not recommended to use one char/ 
[in square brackets] 
{quote me like that} 
2

在我來說,我想補充的報價僅如果字符串是不是已經在引號包圍,所以我所做的:

(這是什麼,我實際上做了稍微不同的,所以這是未經測試)

public static string SurroundWith(this string text, string ends) 
{ 
    if (!(text.StartsWith(ends) && text.EndsWith(ends))) 
    { 
     return string.Format("{1}{0}{1}", text, ends); 
    } 
    else 
    { 
     return text; 
    } 
}