简体   繁体   English

如何在asp.net c#中的另一个文本框中拆分电子邮件ID并显示@之前的文本

[英]how can i split email id and display the text before @ in another textbox in asp.net c#

It is a web application i have login it some fields like name,email id,login name like that but i need to split the email id @ before text it will display automatically in login name text box. 这是一个Web应用程序,我已经登录了一些字段,如名称,电子邮件ID,登录名称,但我需要在文本之前拆分电子邮件ID @它将自动显示在登录名文本框中。 when i entered email id split and display another name another text box like this: 当我输入电子邮件ID拆分并显示另一个名称时,另一个文本框如下:

i want to display like this 我想要这样显示

 eamil id: xyz@gmail.com login name: xyz 

i tried this code but not working: 我尝试了这段代码,但没有工作:

String str= txtemailid.Text;
String[] name=str.split('@');
txtloginname.Text=name[0].Tostring();

can any one help me out 谁能帮我吗

Here is working example ; 这是工作的例子 ; It is better to check for the array Length after split since there may be a chance for entering wrong input, that may lead to exception in case of invalid input; 最好在拆分后检查数组长度,因为可能有输入错误的输入,这可能导致输入无效时出现异常;

string str= "xyz@gmail.com";
string[] name=str.Split('@');
if (name.Length > 1)
 {
    Console.WriteLine("login name:{0}", name[0]);
    Console.WriteLine("login Domain:{0}", name[1]);  
 }

I thing You have not compile your code; 我没有编译你的代码; since it having lots of errors; 因为它有很多错误;

split() will be .Split() S is capital; split()将是.Split() S是资本; Tostring() will be ToString() here also S is capital; Tostring()将是ToString()这里S也是资本; Here .ToString() is not necessary since name is a string array. 这里.ToString()不是必需的,因为name是一个字符串数组。 And one more Thing in C# String and string are Different ; 而且C#中的一个字符串和字符串是不同的 ; so use string to declare variables; 所以使用string来声明变量;

var email = "xyz@gmail.com";
var name = email.Substring(0, email.IndexOf('@'));
Console.WriteLine(name);

Simply without index of array. 简单没有数组索引。

string email = "xyz@gmail.com";
var name = email.Split('@')[0];

This works, but split is called with capital S. C# is case sensitive. 这是有效的,但是使用大写字母S调用split .C#区分大小写。

You only need to change to capital "S" in two words (Split and ToString). 您只需要用两个单词(Split和ToString)更改为大写“S”。 If you do it, your code will work: 如果你这样做,你的代码将起作用:

String str = txtemailid.Text;
String[] name = str.Split('@');
txtloginname.Text = name[0].ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM