473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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 />
Jul 26 '06 #1
8 4390
"Harvey Triana" <ha**********@hotmail.comha scritto nel messaggio
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 :)

Jul 26 '06 #2
Are there another approach?

Let me a sample, please.

<Harvey Triana />

"Fabio" <zn*******@virgilio.itescribió en el mensaje
news:e4**************@TK2MSFTNGP04.phx.gbl...
"Harvey Triana" <ha**********@hotmail.comha scritto nel messaggio
>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 :)

Jul 26 '06 #3
"Harvey Triana" <ha**********@hotmail.comwrote in message
news:uo*************@TK2MSFTNGP03.phx.gbl...
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...%20Methods.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...?
Jul 26 '06 #4
Harvey Triana wrote:
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
Jul 26 '06 #5
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 />

"John B" <jb******@yahoo.comescribió en el mensaje
news:44**********@news.iprimus.com.au...
Harvey Triana wrote:
>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

Jul 27 '06 #6
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 />

"Mark Rae" <ma**@markNOSPAMrae.comescribió en el mensaje
news:%2****************@TK2MSFTNGP04.phx.gbl...
"Harvey Triana" <ha**********@hotmail.comwrote in message
news:uo*************@TK2MSFTNGP03.phx.gbl...
>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...%20Methods.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...?

Jul 27 '06 #7
Harvey Triana <ha********@etb.com.cowrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 27 '06 #8
Excelent. Thanks.

<Harvey Triana />

"Jon Skeet [C# MVP]" <sk***@pobox.comescribió en el mensaje
news:MP************************@msnews.microsoft.c om...
Harvey Triana <ha********@etb.com.cowrote:
>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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jul 27 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Martin Euredjian | last post by:
I could use a shove in the right direction... I'm using the Dreamweaver MX suite to build a website for my business. At first I threw something together quickly just to get going. I now need...
20
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into...
9
by: Mike MacSween | last post by:
So in preparing an export routine to send stuff to the courier company's label printing software (which is surprisingly rough, for a big company), I've discovered a few carriage returns in fields...
8
by: A.M | last post by:
Hi, Using C#, what is the equivalent of functions Left, Right and Mid that we had in vb6? Thanks, Alan
7
by: ReidarT | last post by:
How do I use left, mid and right in vb.net? regards reidarT
12
by: patang | last post by:
This is really silly question. Dim s As String s = "the" s = Right(s, 2) Why this gives me compilation error? (This works in VB6 .. what do I have to do to make it work in VB.Net) I have...
3
by: sck10 | last post by:
Hello, What are the equivalent functions for Right and Left in c#? Thanks, sck10
1
by: Simon | last post by:
Dear readers, Is there some one who knows the solution of the problem caused by the reference libraries in the VBA code. If you run an application on an alien PC it seems everything is OK....
2
by: bleslie | last post by:
Having a bit of a problem i need to extract the state code out of a field formated like this. WEST BEND, WI 53095 I've managed to get the city and zip code in sperate fields using the right...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.