Approach Right(), Left(), Mid(), and so on. In C#

H

Harvey Triana

Hello.

By example, I have string with a "Hello There". I use Right("Hello There",
3) function in VB.NET to just get "ere" out of the string but there doesn't
seem to be one in C#.

I have two approach:

1) write something as:

public static string Right(string s, int n) {
if (n > s.Length) {
return s;
}
else if (n < 1) {
return "";
}
else {
return s.Substring(s.Length - n, n);
}
}

.. and put then into library

2) Use Microsoft.VisualBasic namespace into C# application. .. (to be C is
not seen elegant)

My quiestion is: -
Which approach do you prefer?
Which approach has better performance?

<Harvey Triana />
 
F

Fabio

Harvey Triana said:
By example, I have string with a "Hello There". I use Right("Hello There",
3) function in VB.NET to just get "ere" out of the string but there
doesn't seem to be one in C#.

In VB.Net such as in c# you should avoid old VB6 coding style.
Take a look at the string class, it has all the methods you need and much
more :)
 
M

Mark Rae

Are there another approach?

Another approach? In .NET there's always another approach....

You've already answered your own question. You have three approaches:

1) Use the built-in string class - that's what it's for.
http://www.tangiblesoftwaresolutions.com/Articles/CSharp Equivalent to VB String Methods.htm

2) Re-invent the wheel and write your own function - that way, you can use
20 lines of code intead of 1 line of code.

3) Use the Microsoft.VisualBasic namespace - in fact, why not just stick
with VB6 and don't bother with .NET...?
 
J

John B

Harvey said:
Hello.

By example, I have string with a "Hello There". I use Right("Hello There",
3) function in VB.NET to just get "ere" out of the string but there doesn't
seem to be one in C#.
Try String.SubString
string ht = "hello there";
ht.SubString(ht.length - 4,3);

<...>


JB
 
H

Harvey Triana

Thanks to all
Try String.SubString
string ht = "hello there";
ht.SubString(ht.length - 4,3);

Maybe it is the best pure c# approach, but it is limited,
it is near to "return s.Substring(s.Length - n, n);" but it is not filtered
by n

<Harvey Triana />
 
H

Harvey Triana

Mark-

My original question is "Which approach has better performance?", say 1) my
custom library or 2) namespace Micrososft.VisualBasic

-- Micrososft.VisualBasic has to be heavy, -isn't it?.

<Harvey Triana />
 
J

Jon Skeet [C# MVP]

Harvey Triana said:
Mark-

My original question is "Which approach has better performance?", say 1) my
custom library or 2) namespace Micrososft.VisualBasic

-- Micrososft.VisualBasic has to be heavy, -isn't it?.

In many cases it is, but Right is actually reasonably efficient.
There'll be the extra baggage of loading the DLL in the first place,
and you're less likely to be compatible with other CLRs such as Rotor
and Mono, but the performance isn't likely to be an issue.

Personally I'd stick my own implementation into a string utility class
- there are bound to be utility calls you want to be able to make which
*aren't* covered by the VB assembly. It also means you can determine
the desired behaviour yourself - your implementation behaves
differently to the VB one when you pass in a negative length, for
instance.
 
H

Harvey Triana

Excelent. Thanks.

<Harvey Triana />

Jon Skeet said:
In many cases it is, but Right is actually reasonably efficient.
There'll be the extra baggage of loading the DLL in the first place,
and you're less likely to be compatible with other CLRs such as Rotor
and Mono, but the performance isn't likely to be an issue.

Personally I'd stick my own implementation into a string utility class
- there are bound to be utility calls you want to be able to make which
*aren't* covered by the VB assembly. It also means you can determine
the desired behaviour yourself - your implementation behaves
differently to the VB one when you pass in a negative length, for
instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top