1

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. when i entered email id split and display another name another text box like this:

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

sujith karivelil
  • 26,861
  • 6
  • 46
  • 76

4 Answers4

2

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; Tostring() will be ToString() here also S is capital; Here .ToString() is not necessary since name is a string array. And one more Thing in C# String and string are Different; so use string to declare variables;

Community
  • 1
  • 1
sujith karivelil
  • 26,861
  • 6
  • 46
  • 76
1
var email = "xyz@gmail.com";
var name = email.Substring(0, email.IndexOf('@'));
Console.WriteLine(name);

Simply without index of array.

Denis Bubnov
  • 1,985
  • 4
  • 23
  • 47
0
string email = "xyz@gmail.com";
var name = email.Split('@')[0];

This works, but split is called with capital S. C# is case sensitive.

Pedro G. Dias
  • 3,058
  • 1
  • 12
  • 25
  • where should i take it in the text box event in that email id text box event or login name text box event. could you please tell me which event i should take –  Apr 01 '16 at 05:07
  • If this is a web application, then you're basically left with 2 options, you can either do this in JavaScript and react to some changed() event, or you can do it when you submit the form, and check it with C# as in the example given above. This is, however a completely different question than the original one. If you're happy with the answer, please click on the checkmark :) – Pedro G. Dias Apr 01 '16 at 05:12
  • Great Suggestion : " please click on the checkmark :" http://meta.stackexchange.com/questions/14994/do-you-feel-dirty-if-you-nudge-new-users-to-accept-your-answer-when-they-indicat – sujith karivelil Apr 01 '16 at 05:23
  • Thank you very much it help me a lot –  Apr 01 '16 at 05:27
0

You only need to change to capital "S" in two words (Split and ToString). If you do it, your code will work:

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