2013-03-27 17 views
2

我正在使用visual studio 11.0和.Net網絡編程我想將字符串inputed從TextBox1轉換爲TitleCase,sententenceCase, UpperCase和小寫,從RadioButtonList1中選擇並在Label1.Text中顯示結果。但我不希望我的單詞在引號內被轉換。例如「ASP.NET」,「Ph.D」和「xyz」 我已經完成了標題大寫,大寫和小寫的編碼,但是我希望這個代碼在無論「quites」出現時都被忽略/跳過或者過濾。將字符串轉換爲TitleCase,SentenceCase,UpperCase和LowerCase,但忽略大小寫,如果單詞在「引號」內部

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Globalization; 

public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 

    } 
    private string ConvertToTitleCase(string val) 
    { 
    string returnString = string.Empty; 

System.Globalization.CultureInfo info = System.Threading.Thread.CurrentThread.CurrentCulture; 

TextInfo textInfo = info.TextInfo; 

returnString = textInfo.ToTitleCase(val); 

return returnString; 
} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
if (RadioButtonList1.SelectedValue == "a") 
    { 
Label1.Text = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text); 
Label1.Text = ConvertToTitleCase(TextBox1.Text); 

TextBox1.Text.Equals(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase); 
    } 
else if (RadioButtonList1.SelectedValue == "b") 
    { 
Label1.Text = "you have selected b"; 
    } 
else if (RadioButtonList1.SelectedValue == "c") 
    { 
Label1.Text = TextBox1.Text.ToUpper(); 
    } 
else 
Label1.Text = TextBox1.Text.ToLower(); 

} 

我需要一個提示或代碼,將忽略首字母大寫,SentenceCase,大寫和小寫如果.. 我strr​​ing是裏面的「報價」

例:

字符串TextBox1的=您好,這是「ASP .net「。你是」B.Tech「並歡迎加入」HCT「。

輸出:

TitleCase:你好,這是「asp.net」。你在「B.Tech」並歡迎來到「HCT」。

判詞:您好,這是「asp.net」。您身處「B.Tech」,歡迎來到「HCT」。

大案:你好這是「asp.net」。你是「B.Tech」,歡迎來到「HCT」。

LowerCase:你好,這是「asp.net」。你在「B.Tech」,並歡迎來到「HCT」。

+0

確定...和[你嘗試過什麼?](http://whathaveyoutried.com) – Blachshma 2013-03-27 23:34:01

+0

我的問題是清楚的我認爲 – baluchi55 2013-03-27 23:49:35

+1

我不是故意與你爭論,但問題不是那麼清楚。可以取悅你展示一些你寫的代碼,顯示你正在做什麼,哪些不起作用?另外,考慮到你是一位新用戶,並且擁有超過8K聲望的用戶(@Blachshma)提出建議,您應該聽取建議。 – 2013-03-28 00:07:18

回答

1

我會考慮使用一個字符串包含方法它返回一個布爾值。您可以檢查該字符串是否包含引號,然後您可以在引號上拆分字符串,並將您想要的位轉換並保持原樣。如果不是我道歉,我希望我能正確理解。

字符串文檔包含。 http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx 字符串拆分的文檔。 http://msdn.microsoft.com/en-us/library/system.string.split.aspx

希望這會有所幫助。

只是在你所發佈的那個課堂上玩耍,沒有使用過那個。

using System; 
using System.Globalization; 
using System.Threading; 

    public class FilterString{ 
    public static void Main(string[] args) 
    { 
     CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; 
     TextInfo textInfo = cultureInfo.TextInfo; 

     string textBoxText = "tEsting To upPerCasE 'STAYCAPS'"; 
     string filterdTextForLabel = textInfo.ToTitleCase(textBoxText) ; 
     Console.WriteLine(filterdTextForLabel); 

    } 
} 

這使用單引號出現它會返回結果,你會喜歡他們。

輸出:測試爲大寫「STAYCAPS」

但我當時的想法是,你可以做一些過濾你做轉換前分配用於文本輸入的變量再拆上的報價和任何字符串中間部分留下你可以稱冠的案例。讓我知道如果你不能得到它的工作,我會做出更深入的迴應。 :d

+0

我在我的程序中添加了此類,以便它接受標題轉換 – baluchi55 2013-03-28 00:36:59

+0

使用System.Globalization; – baluchi55 2013-03-28 00:37:52

+0

Label1.Text = System.Globalization.cultureInfo.currentCuture.TextInfo.ToTitleCase(TextBox1.Text); – baluchi55 2013-03-28 00:41:16

0
private delegate string ConvertFunc(string input); 

private string ModifyString(string input, ConvertFunc conversion) 
{ 
    MatchCollection matches = Regex.Matches(input, "\".*?\""); 
    int lastPos = 0; 
    StringBuilder stringBuilder = new StringBuilder(input.Length); 
    foreach (Match match in matches) 
    { 
     int currentPos = match.Index; 
     string toConvert = input.Substring(lastPos, currentPos - lastPos); 
     string converted = conversion(toConvert); 
     stringBuilder.Append(converted); 
     stringBuilder.Append(match.Value); 
     lastPos = currentPos + match.Length; 
    } 

    if (lastPos < input.Length) 
    { 
     stringBuilder.Append(conversion(input.Substring(lastPos))); 
    } 

    return stringBuilder.ToString(); 
} 

private string ToUpper(string toConvert) 
{ 
    return toConvert.ToLower(); 
} 

然後從你的代碼中調用ModifyString方法:

string modifiedString = ModifyString("This can be converted \"This cannot be converted\"", ToUpper); 
+0

我正在編譯錯誤消息:期望的類,委託,枚舉,接口或結構。 – baluchi55 2013-03-29 10:38:17

+0

你可以請更新我發佈的代碼。因爲我是初學者,並不想與不同班級一起工作。謝謝@Petr Behensky – baluchi55 2013-03-29 10:40:35

+0

您使用的是哪個版本的.NET Framework? – 2013-03-29 11:01:38

相關問題