i need to extract the company name from an email inside my asp.net mvc web application:-
for exmaple if i have an email address = [email protected]
to get Mycompanyname
with first letter capital?
BR
To get a part of a string, string. substring() method is used in javascript. Using this method we can get any part of a string that is before or after a particular character.
To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.
Use the substring() method to get the substring before a specific character, e.g. const before = str. substring(0, str. indexOf('_')); . The substring method will return a new string containing the part of the string before the specified character.
Java String trim() The Java String class trim() method eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in Java string checks this Unicode value before and after the string, if it exists then the method removes the spaces and returns the omitted string.
string address = "[email protected]";
string name = address.Split('@')[1].Split('.')[0];
name = name.Substring(0,1).ToUpper() + name.Substring(1); // Mycompanyname
Another option to get name is regular expression:
var name = Regex.Match(address, @"@([\w-]+).").Groups[1].Value
To get rid of the @ and everything before that, you would use something like this in your particular case:
string test = "[email protected]";
test = test.Substring(test.IndexOf('@')+1, test.IndexOf(".") -(test.IndexOf('@')+1));
MessageBox.Show(test);
And this explains how to make the first letter a capital, which you would use after you strip out the @ and .com parts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With