是否有一些在c#中的字符串周圍添加引號的方法?c#中的字符串自動引號 - 內置方法?
回答
你的意思是只是加引號?喜歡這個?
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
我不能說我需要做的這往往足以令它值得擁有雖然方法...
燁s表示,採用級聯和轉義字符
myString = "\"" + myString + "\"";
也許擴展方法
public static string Quoted(this string str)
{
return "\"" + str + "\"";
}
用法:
var s = "Hello World"
Console.WriteLine(s.Quoted())
沒有,但你可以寫你自己的,或者創建一個擴展方法
string AddQuotes(string str)
{
return string.Format("\"{0}\"", str);
}
擴展方法看起來不錯,謝謝。 – Darqer 2011-04-21 15:14:40
string quotedString = string.Format("\"{0}\"", originalString);
使用轉義字符
只是前面加上一個反斜槓,這被稱爲一個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
沒有這樣的內置的方法做你的要求
有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));
}
我是新手我自己的一點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}
在我來說,我想補充的報價僅如果字符串是不是已經在引號包圍,所以我所做的:
(這是什麼,我實際上做了稍微不同的,所以這是未經測試)
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;
}
}
- 1. 方案中來自雙引號符號的字符串數據
- 2. 在C++中讀引號的字符串
- 3. 在C#中的字符串文字內部轉義雙引號#
- 4. 從字符串c中刪除單引號字符C++
- 5. C#字符串中的雙引號
- 6. C#在字符串中傳遞引號
- 7. 在C#字符串中轉義引號
- 8. 'u0022'引入字符串引號C#
- 9. 如何傳遞C++中的雙引號內的字符串
- 10. C++ - 用雙引號將字符串替換爲帶雙引號的字符串
- 11. 字符串中的引號
- 12. 轉換單引號字符串中雙引號字符串
- 13. 自動寫入內容c#字符串
- 14. 替換字符串中的子字符串,除非字符串在引號內
- 15. 引號內的Python字符串賦值
- 16. 插入字符串中的C#引號意外字符\ 0022
- 17. c#內部字符串數組方法
- 18. C++用逗號和字符串CSV線雙引號內
- 19. 查找字符串中的引號內的字符
- 20. 自動刪除JSON字符串中的引號Java
- 21. 替換字符串C#中的引號字符不工作
- 22. C# - 。載 - 在字符串有引號它
- 23. 解析某個字符串後引號內的字符串
- 24. 在Word中自動換行,但不包括在引號內的字符串
- 25. 單引號和雙引號字符串中的特殊字符
- 26. Java內置方法 - 在字符串中出現char字符
- 27. Python中三引號字符串的替代方法Python有
- 28. 查找字符串/符號/方法的引用
- 29. 雙引號字符串替換在C#
- 30. 逃生雙引號中的字符串內撇號(JavaScript)的
你能否澄清一下你的問題! – lKashef 2011-04-21 15:09:58
from string abc我想讓字符串「abc」,我想知道是否有一些可以完成這個工作的類中的方法。我知道它是5行代碼來編寫你自己的。 – Darqer 2011-04-21 15:12:28
@ Darger:如果字符串包含引號會怎麼樣?你會用它做什麼? – 2011-04-21 19:52:00