Reputation: 21
I am using visual studio 11.0 and in .Net web programming I want to convert a string inputed from TextBox1 to TitleCase, sententenceCase, UpperCase and lowercase by selecting from RadioButtonList1 and show the result in Label1.Text .But I don't want my words which are inside quotation marks to be converted. Example “ASP.NET", "Ph.D" and "xyz". I have done coding for title case, uppercase and lowercase but i want this code to be ignored/skipped or filtered whereever "quites" comes.
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();
}
I need a hint or code which will ignore TitleCase, SentenceCase, UpperCase and LowerCase If.. my strring is inside "quotes".
Example:
String TextBox1 = hellO thIs is "asp.net". you ARE in "B.Tech" and welcome To "HCT".
Output:
TitleCase: Hello This Is "asp.net". You Are In "B.Tech" And Welcome To "HCT".
SentenceCase: Hello this is "asp.net". You are in "B.Tech" and welcome to "HCT".
UpperCase: HELLO THIS IS "asp.net". YOU ARE IN "B.Tech" AND WELCOME TO "HCT".
LowerCase: hello this is "asp.net". you are in "B.Tech" and welcome to "HCT".
Upvotes: 2
Views: 8001
Reputation: 620
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();
}
Then call ModifyString method from your code:
string modifiedString = ModifyString("This can be converted \"This cannot be converted\"", ToUpper);
Upvotes: 0
Reputation: 1652
I would consider using a string contains method it returns a boolean. You could check whether the string contains quotes then you could split the string on the quote and convert the bits you want to and leave the rest as is. I hope I am understanding correctly if not I apologize.
Document for string contains. http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx Document for string split. http://msdn.microsoft.com/en-us/library/system.string.split.aspx
Hope this helps.
Just playing around with that class you posted haven’t used that one before.
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);
}
}
This using single quotes it appears it returns the results like you would like them.
output: Testing To Uppercase 'STAYCAPS'
But what I was thinking is you could do some filtering before you make the conversion assign a variable for the text input then split the string on the quote and anything in the middle portion leave the same the rest you could title case. Let me know if you cannot get it to work I'll make a more in-depth response. :D
Upvotes: 1