将全名转换为缩写名 [英] convert the fullname to abbreviated name
本文介绍了将全名转换为缩写名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的qn是我在一个文本框中输入了全名,我想在单击提交"按钮时在另一个文本框中显示简称.
例如:-alia ankit sahoo
结果应该是a.a.sahoo
my qn is i have a written a full name in one text box i want to display the short name in another textbox when clicking on submit button.
for e.g:-alia ankit sahoo
result should be a.a.sahoo
推荐答案
假设空格是唯一的分隔字符,则可以尝试:
Assuming that space is the only delimiting character, you could try:
string s = "alia ankit sahoo";
string[] words = s.Split(' ');
for (int i = 0; i < words.Length - 1; i++)
{
words[i] = words[i].Substring(0, 1);
}
s = string.Join(".", words);
1)将全名复制到字符串变量中
2)使用.ToCharArray()
将字符串变量转换为字符数组 3)使用一些逻辑从该字符数组中获得所需的结果
1) Copy full name into a string variable
2) Convert that string variable to Character Array using .ToCharArray()
3) Use some logic to get desired result from that character array
尝试以下代码段:
try following code snippet :
String[] strShortName = TextBox1.Text.Split(' ');
string str1 = "";
for (Int16 i = 0; i < strShortName.Length; i++)
{
if (i < 2)
{
str1 = str1 + strShortName[i].ToString().Substring(0,1) + ".";
}
else
{
str1 = str1 + strShortName[i];
}
}
TextBox2.Text = str1;
这篇关于将全名转换为缩写名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文