Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a substring after certain character

Tags:

c#

asp.net

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

like image 479
john Gu Avatar asked Jan 04 '13 01:01

john Gu


People also ask

How do you get part of a string after?

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.

How do you split a string at a certain 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.

How do you get a substring that comes before a certain character?

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.

How do you trim a string after a specific character in Java?

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.


2 Answers

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
like image 57
Sergey Berezovskiy Avatar answered Oct 05 '22 16:10

Sergey Berezovskiy


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.

like image 36
FrostyFire Avatar answered Oct 05 '22 16:10

FrostyFire