如何获得数组长度? [英] how to get lenght of array?
本文介绍了如何获得数组长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取数组的长度意味着数组中有多少个单词?
对于名称提取,我使用了数组来拆分变量,但是当我只输入名字时,它会给出错误,因此我认为首先我们要获取变量的数量,然后进行匹配.
所以请帮帮我.
我的代码是:-
How to get the length of array means how much words in array?
For name fetching I used array for splitting variable, but when I put only first name then it gives error so I think first we get count of variable then match.
so please help me.
My code is:-
String[] Spilted = Name.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
String firstname = Spilted[0];
String lastname = Spilted[1];
SqlCommand cmd = new SqlCommand("Select PROFILE_ID,FIRST_NAME,PROFILE_REQUEST_STATUS from DSProfile.HDR_PROFILE where FIRST_NAME='" + firstname + "' and LAST_NAME='" + lastname + "' and PROFILE_REQUEST_STATUS='" + false + "'", con);
// SqlCommand cmd = new SqlCommand("Select p.PROFILE_ID,g.FRIEND_ID,p.FIRST_NAME,g.ACCEPT_STATUS from DSProfile.HDR_PROFILE p,DSMailBox.HDR_GROUP g where p.FIRST_NAME='" + friendname + "' and p.PROFILE_ID=g.FRIEND_ID and ACCEPT_STATUS='" + false + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
int cnt = ds.Tables[0].Rows.Count;
DT = ds.Tables[0];
GridView1.DataSource = ds;
GridView1.DataBind();
推荐答案
使用 ^ ]属性:
Use the Length[^] property:
if( Spilted.Length == 1)
{
// Code for first name only.
}
else
if( Spilted.Length == 2)
{
// Code for first and last name.
}
尝试一下
String Name = "first last";
String[] Spilted = Name.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
String firstName, lastName;
if (Spilted.Length == 2)
{
firstName = Spilted[0];
lastName = Spilted[1];
}
else if (Spilted.Length == 1)
firstName = Spilted[0];
也尝试此操作,我想您想对整个字符串中的单词进行计数...
Try this also i think you want to count words in the whole string...
int count = 0;
string st = "this is your string including spaces and we are counting spaces count plus one is your words in the sentance ";
foreach (char c in st.Trim())
{
if (char.IsSeparator(c))
{
count++;
}
}
int totWords = count + 1;
lblResult.Text = count.ToString();
我知道这将帮助您100%
谢谢.
i know it will helps you 100%
Thank you.
这篇关于如何获得数组长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文