vb.net 将字符串转换为 TitleCase、SentenceCase、UpperCase 和 LowerCase,但如果单词在“引号”内,则忽略大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15671510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:00:38  来源:igfitidea点击:

convert string to TitleCase, SentenceCase, UpperCase and LowerCase but ignore case if words are inside "quotation marks"

c#asp.net.netvb.net

提问by baluchi55

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.

我正在使用 Visual Studio 11.0,在 .Net 网络编程中,我想通过从 RadioButtonList1 中选择,将从 TextBox1 输入的字符串转换为 TitleCase、sententenceCase、大写和小写,并在 Label1.Text 中显示结果。但我不想要我的话要转换的引号内。示例“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();

}

I need a hint or code which will ignore TitleCase, SentenceCase, UpperCase and LowerCase If.. my strring is inside "quotes".

我需要一个提示或代码,它会忽略 TitleCase、SentenceCase、UpperCase 和 LowerCase 如果……我的字符串在“引号”内。

Example:

例子:

String TextBox1 = hellO thIs is "asp.net". you ARE in "B.Tech" and welcome To "HCT".

String TextBox1 = hellO 这是“asp.net”。你在“B.Tech”,欢迎来到“HCT”。

Output:

输出:

TitleCase: Hello This Is "asp.net". You Are In "B.Tech" And Welcome To "HCT".

TitleCase:您好,这是“asp.net”。你在“B.Tech”,欢迎来到“HCT”。

SentenceCase: Hello this is "asp.net". You are in "B.Tech" and welcome to "HCT".

SentenceCase:您好,这是“asp.net”。您在“B.Tech”,欢迎来到“HCT”。

UpperCase: HELLO THIS IS "asp.net". YOU ARE IN "B.Tech" AND WELCOME TO "HCT".

大写:你好,这是“asp.net”。您在“B.Tech”并欢迎来到“HCT”。

LowerCase: hello this is "asp.net". you are in "B.Tech" and welcome to "HCT".

小写:你好,这是“asp.net”。你在“B.Tech”,欢迎来到“HCT”。

回答by vikingben

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.aspxDocument for string split. http://msdn.microsoft.com/en-us/library/system.string.split.aspx

字符串包含的文档。 http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx字符串拆分文档。 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'

输出:测试到大写 '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

但是我在想的是,您可以在进行转换之前进行一些过滤,然后再为文本输入分配一个变量,然后在引号上拆分字符串,中间部分的任何内容都保持不变,其余部分可以作为标题大小写。如果你不能让它工作,请告诉我,我会做出更深入的回应。:D

回答by Petr Behensky

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:

然后从您的代码中调用 ModifyString 方法:

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