question on substring

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do i get rid of the "OR " (at the end) from this string?

I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "

Many thanks
 
Hi huzz,

Well, I'm not entirely sure this is the best approach, but if you know
there is "OR " at the end you could do

String s = "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR ";
s = s.Substring(0, s.Length - "OR ".Length);
 
huzz said:
How do i get rid of the "OR " (at the end) from this string?

I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "

Many thanks
 
I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "

string s = "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR ";

if (s.EndsWith(" OR "))
{
// First argument is the start index, second argument is the number of characters.
s = s.Substring(0, s.LastIndexOf(" OR "));
}

Debug.Print(s);

// should print: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "
 
Sorry about that, my news reader had a hickup and I thought for a moment I
saw your question in another group, and that I answered your question
there.

Please ignore.
 
huzz said:
How do i get rid of the "OR " (at the end) from this string?

I have : "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR "
I want: "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' "

Many thanks

Use a combination of LastIndexOf("OR") and Substring methods of the String
class
Peek at MSDN to get more details...
 
huzz,

As alternative

String s = "Keywords LIKE '%awlad%' OR Keywords LIKE '%huzz%' OR ";
s = s.Substring(0, s.LastIndexOf("OR"));

Cor
 
Amazing. Of all my time using .NET I've never seen that function. I looked it up.

It's called TrimEnd(params char[])

So, you can use:

s = s.TrimEnd(' ', 'O', 'r', ' ');

It's case sensitive. It may not be the best approach to hardcode the character literals. You could also do this:

s = s.TrimEnd(" Or ".ToCharArray());

or

string toTrim = " Or ";
s = s.TrimEnd(toTrim.ToCharArray());

Learn something new every day :)

There are mutliple ways of accomplishing the task at hand. The best one depends on your particular situation.
Check out the System.Text.StringBuilder class for a better way to concatinate strings for dynamic SQL queries.
 
RiteshDotNet said:
Best way is u can use string.endtrim("or")

Well, this may easily lead to unwanted results. Imagine following situation:

string s = "blah blah or ro rrroo oorr or ";

now, if you do:

string x = s.TrimEnd(' ', 'o', 'r', ' ');

you will end up with x containing:

x == "blah blah".

This is because TrimEnd() removes _all_ occurences of the provided chars
from the end of the string - regardless of their order.
This is not necessarily problem in OP's case, however it is better to be
aware of that.

Michal Dabrowski
 
if(str.EndsWith("OR "))
{
str = str.SubString(0, str.Length - 3);
}

Well, this may easily lead to unwanted results. Imagine following situation:

string s = "blah blah or ro rrroo oorr or ";

now, if you do:

string x = s.TrimEnd(' ', 'o', 'r', ' ');

you will end up with x containing:

x == "blah blah".

This is because TrimEnd() removes _all_ occurences of the provided chars
from the end of the string - regardless of their order.
This is not necessarily problem in OP's case, however it is better to be
aware of that.


Michal Dabrowski

Otis Mukinfus
http://www.otismukinfus.com
 
Back
Top