Click here to Skip to main content
15,916,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys i want to trim a string before a special character..

lets say the string is str="qwertyuiop@345*7%6&n>>werwer>ABCD"
i want to get the characters after > and ignore the rest.
i.e the resultant string must be restr="ABCD"
Posted
Updated 3-Dec-14 2:23am
v2

You may use String.Split[^] for the purpose.
 
Share this answer
 
You could grab a substring starting from the index of the special character:

C#
string newstring = str.Substring(str.IndexOf(">"));


Or, if it has to be a specified length:

C#
int length = 1;

string newstring = str.Substring(str.IndexOf(">"), length);
 
Share this answer
 
v3
Comments
Renjith_R 3-Dec-14 8:24am    
Hi, Thanks for your reply
My string is str="qwertyuiop@345*7%6&n>>werwer>ABCD
I need to get characters after last >
Thanks in advance
Milfje 3-Dec-14 8:53am    
There's also a string.LastIndexOf() method which accepts either a string or a char.
Hi,

or do it the Regex way.

C#
string str = "qwertyuiop@345*7%6&n>ABCD";

string pattern = ".*>(?<result>.*)";

Regex regex = new Regex(pattern);

Match match = regex.Match(str);

if (match.Success)
{
    string result = match.Groups["RESULT"].Value;
}


Best regards,
Stephan
 
Share this answer
 
v2
Try below code.

C#
string str = "qwertyuiop@345*7%6&n>ABCD";
str = str.Substring(str.LastIndexOf(">")+1);
 
Share this answer
 
Comments
Renjith_R 3-Dec-14 8:25am    
Hi, Thanks for your reply
My string is str="qwertyuiop@345*7%6&n>>werwer>ABCD
I need to get characters after last >
Thanks in advance
Praveen Kumar Upadhyay 3-Dec-14 8:27am    
This will give you the string after last >(arrow) only.
Please accept the solution.
Praveen Kumar Upadhyay 3-Dec-14 8:51am    
Thanks a lot for accepting the answer.
Renjith_R 3-Dec-14 9:54am    
Hi How to remove last character after two special character
string str="qwertyuiop@345*7%6&n>>werwer>ABCD
new string should be s= "werwer>ABCD"
Praveen Kumar Upadhyay 3-Dec-14 12:15pm    
str = str.Substring(str.LastIndexOf(">>")+1);
Refer indexof and substring

http://www.dotnetperls.com/indexof[^]



http://www.dotnetperls.com/substring[^]

C#
string str = "qwertyuiop@345*7%6&n>ABCD";
               
string Result = str.Substring((str.IndexOf(">"))+1);
 
Share this answer
 
Comments
Renjith_R 3-Dec-14 8:26am    
Hi, Thanks for your quick reply
My string is str="qwertyuiop@345*7%6&n>>werwer>ABCD
it contain more than one > , I need to get characters after last >
Thanks in advance
King Fisher 3-Dec-14 8:28am    
update your Question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900