Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have string like "Karim or Rahim goes for OR "

I want to get result as "Karim or Rahim goes for"

C#
string str="Karim OR Rahim OR Motin goes for OR ";



I want to remove the last word OR even there can have a space after OR or sometimes it can't.

Please keep in mind that sometimes the last word can be "OR" and sometimes it can be "AND".

So please give a solution how can I remove last word ?

I am triying through this code. By using following code I can get last word. But I don
Posted
Comments
Thanks7872 4-Nov-14 6:51am    
If there is no space then how do you identify that its last word? e.g. Karim or Rahim goes forOR. If you see this statement, the last word would be forOR,not OR.

Do you have fix words 'OR','AND' etc? You said that you are using some code,but i think you forgot to mention the same. Would you please show us the code you have tried so far?
sachi Dash 4-Nov-14 6:52am    
I am actually telling the space after last word. Do you understand now? help me if possible.

Thanks7872 4-Nov-14 6:53am    
That is,in any statement you want to remove the last word,regardless of what it is? Right?

If the last word is always AND or OR then it's pretty simple, just use a regex:
C#
public static Regex regex = new Regex("(\\s+(AND|OR)\\s*)$");
                                    ...
string result = regex.Replace(InputText,"");
 
Share this answer
 
Hi,

First understand that space(' ') is the separator of characters to make those as words.


1.Remove the last extra space from your string..
C#
string str="Karim OR Rahim OR Motin goes for OR ";
      string strTrimmed=str.Trim();


2.Then Get the substring by finding the last space(' ')

C#
string finalString= strTrimmed.Substring(strTrimmed.LastIndexOf(" ",strTrimmed.Length);

LastIndexOf() -- > is used to find the last index position of a specified character..


Thanks
Magesh M
 
Share this answer
 
Try this link http://stackoverflow.com/questions/14825949/replace-last-match-in-string[^]

In this example they have used you would have

string str = "Karim OR Rahim OR Motin goes for OR";

//And then just trim the string after you get your last value removed...
str = ReplaceLastOccurrence(str, "OR", "").Trim();
 
Share this answer
 
There are difeferent way that you can do such operation .
Here first remove the spaces by using Trim() operator and then split the string by spaces.
string[] arr=x.Trim().Split(null);

now remove the last word by using LINQ operation
arr = arr.Where((o, i) => i != arr.Length - 1).ToArray();       

Last you make a Join operator to join the array items and make a string
x = string.Join(" ", arr);

Hope it helps you

thanks
 
Share this answer
 
Try this
C#
str.Substring(0, str.LastIndexOf(" ")<0?0:str.LastIndexOf(" "));


Hope it will help you.
 
Share this answer
 

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