在一个字符串中捕获第一个单词时遇到了一个错误 [英] Had A Bug In Captilizing The First Word In A String

查看:64
本文介绍了在一个字符串中捕获第一个单词时遇到了一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码,用于将字符串的每个单词中的第一个字母大写插入/输入TextBox ..代码很好而且很好...

但是当我尝试输入时在字符串之间的字母/单词,然后光标跳到字符串的末尾..我已经尝试了很多......请任何人帮我这个...

这个代码在C#中,Winforms:

  private   void  txtCName_KeyPress( object  sender,KeyPressEventArgs e)
{
try
{
bool hasSingleWhitespace = txtCName.Text == ;
if (!(Char.IsLetter(e.KeyChar)|| Char.IsControl(e.KeyChar)|| Char.IsWhiteSpace(e.KeyChar) ))
e.Handled = true ;
char [] c = txtCName.Text.ToCharArray();
int j;
for (j = 0 ; j < ; txtCName.Text.Length; j ++)
{
if (j == 0 )c [j] = c [j] .ToString()。ToUpper()[ 0 ];
else c [j] = c [j] .ToString()。ToLower()[ 0 ];
}
txtCName.Text = new string (c);
txtCName.Select(txtCName.Text.Length, 1 );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);

}
}

解决方案

而不是在 KeyPress 事件,最好在验证事件中进行。它将在您验证文本框(选项卡或输入)时执行,而不是每次按下此控件上的键时执行。



如果有帮助,请投票。


这可以用几行代码完成。

使用System.Text.RegularExpressions添加

;



编写此代码



var phrase =Test WORD;

var rx = new System .Text.RegularExpressions.Regex(@"(?< = \w)\w");

var ConvertedValue = rx.Replace(phrase,new MatchEvaluator(m => m) .Value.ToLowerInvariant()));


尝试这个..





< pre lang =c#> CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;

Console.WriteLine(textInfo.ToTitleCase(title));





拜托,请标记为答案if它很有帮助。



很乐意提供帮助:)


The Below Is My Code For Capitalizing The First Letter In Each Word Of A String Insert/Typed In The TextBox ..The Code Is Well And Good...
But When I Tried To Type A Letter/Word In The Between The String,Then The Cursor Is Jumping To The End Of The String..I Had Tried A Lot..Pls Can Any One Help Me In This...
This Code Is In C#,Winforms:

private void txtCName_KeyPress(object sender, KeyPressEventArgs e)
       {
           try
          {
          bool hasSingleWhitespace = txtCName.Text == " ";
              if (!(Char.IsLetter(e.KeyChar) || Char.IsControl(e.KeyChar) || Char.IsWhiteSpace(e.KeyChar)))
               e.Handled = true;
          char[] c = txtCName.Text.ToCharArray();
            int j;
           for (j = 0; j < txtCName.Text.Length; j++)
           {
               if (j == 0) c[j] = c[j].ToString().ToUpper()[0];
                 else c[j] = c[j].ToString().ToLower()[0];
           }
             txtCName.Text = new string(c);
            txtCName.Select(txtCName.Text.Length, 1);
          }
         catch (Exception ex)
        {
              MessageBox.Show(ex.Message);

         }
    }

解决方案

Rather than executing your code in the KeyPress event, better do it in the Validating event. It will be executed when you validate your textbox (tab or enter) rather than each time you press a key on this control.

Please vote if this helped.


this can be done with few lines of code only.
Add
using System.Text.RegularExpressions;

Write This Code

var phrase = "Test WORD";
var rx = new System.Text.RegularExpressions.Regex(@"(?<=\w)\w");
var ConvertedValue = rx.Replace(phrase, new MatchEvaluator(m => m.Value.ToLowerInvariant()));


try this one..


CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;

Console.WriteLine(textInfo.ToTitleCase(title));



please, do mark as answer if it helps.

happy to help :)


这篇关于在一个字符串中捕获第一个单词时遇到了一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆