如何计算C#中的字符? [英] How to counting characters in C# ?

查看:71
本文介绍了如何计算C#中的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

请让我知道,案例是



i有一个包含20个字符的文本字段,我已经拆分了通过使用子字符串方法再次将其作为14 + 6存储在数据库表中的两个不同字段中。

现在问题是当我在文本字段中传递少于14个或少于6个字符时它会给出长度异常,因为在子字符串中它分别要求14和6长度。



i需要知道如何在文本字段中传递少于14和少于6的值。 />


我尝试过:



string fullname = Name_txt.Text;

string val2 = fullname.Substring(0,14); // valid

string val3 = fullname.Substring(14,6);



现在用于子串(val2)1 14是固定的我去输入13个字符它将抛出异常并且如果我输入少于6个charcacter然后它将给出长度计数的异常以及

Hi all,
Please let me know, the case is

i have a text field which contains 20 characters, i have split it again as 14+6 by using substring method which is store in two different fields in database table.
Problem is now when i pass less that 14 or less than 6 characters in text fields it gives length exception, because in substring it demanded for 14 and 6 length respectively.

i need to know how can i pass less than 14 and less than 6 value in text field.

What I have tried:

string fullname = Name_txt.Text;
string val2 = fullname.Substring(0, 14); //validity
string val3 = fullname.Substring(14,6);

now for substring (val2) 1 14 is fixed if i go for entering 13 character it will throw exception and same if i go to enter less than6 charcacter then it will give exception of length count as well

推荐答案

我认为它如果您使用两个文本框会更容易,一个用于名字,一个用于姓氏。

另一个想法是在名字和姓氏之间使用分隔符,例如一个连字符,然后使用 Split()函数。
I think it would be a lot easier if you used two textboxes, one for the first name and one for the last name.
Another idea is to use a separator between first and last name, e.g. a hyphen, and then use the Split() function.


只验证文本的长度

just validate the length of the text
string fullname = Name_txt.Text;
      string val2, val3;
      if (fullname.Length >= 20)
      {
          val2 = fullname.Substring(0, 14);
          val3 = fullname.Substring(14, 6);
      }
      else {
          // show validation message to the user to enter 20+ characters
      }


这篇关于如何计算C#中的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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