我使用的是visual Studio11.0,在.Net web编程中,我想要将从TextBox1输入的字符串转换为TitleCase、sententenceCase、UpperCase和小写,方法是从RadioButtonList1中选择并在Label1中显示结果。the .But我不希望我的引号内的单词被转换。例如“ASP.NET”,"Ph.D“和"xyz”。我已经完成了标题大小写的编码,但是我希望这个代码在"quites“出现的地方被忽略/跳过或过滤。
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();
}我需要一个提示或代码,将忽略TitleCase,SentenceCase,UpperCase和LowerCase,如果..我的字符串在“引号”中。
示例:
String TextBox1 = hellO这是"asp.net“。你在"B.Tech“中,欢迎来到"HCT”。
输出:
TitleCase:您好,我是asp.net。您在"B.Tech“中,欢迎来到"HCT”。
SentenceCase:您好,我是asp.net。你在"B.Tech“中,欢迎来到"HCT”。
UpperCase:您好,我是asp.net。您在"B.Tech“中,欢迎来到"HCT”。
LowerCase:您好,我是asp.net。你在"B.Tech“中,欢迎来到"HCT”。
发布于 2013-03-28 08:14:01
我会考虑使用字符串包含方法,它返回一个布尔值。你可以检查字符串是否包含引号,然后你可以在引号上拆分字符串,并转换你想要的位,让其余的保持原样。我希望我的理解是正确的,如果不是的话,我道歉。
字符串包含的文档。用于字符串拆分的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
发布于 2013-03-29 17:58:02
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);https://stackoverflow.com/questions/15671510
复制相似问题