PC Review


Reply
Thread Tools Rate Thread

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

 
 
Harvey Triana
Guest
Posts: n/a
 
      26th Jul 2006
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 />


 
Reply With Quote
 
 
 
 
Fabio
Guest
Posts: n/a
 
      26th Jul 2006
"Harvey Triana" <(E-Mail Removed)> ha 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



 
Reply With Quote
 
Harvey Triana
Guest
Posts: n/a
 
      26th Jul 2006
Are there another approach?

Let me a sample, please.

<Harvey Triana />

"Fabio" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed)...
> "Harvey Triana" <(E-Mail Removed)> ha 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
>
>
>



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      26th Jul 2006
"Harvey Triana" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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...?


 
Reply With Quote
 
John B
Guest
Posts: n/a
 
      27th Jul 2006
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
 
Reply With Quote
 
Harvey Triana
Guest
Posts: n/a
 
      27th Jul 2006
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" <(E-Mail Removed)> escribió en el mensaje
news:44c7f8c7$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Harvey Triana
Guest
Posts: n/a
 
      27th Jul 2006
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" <(E-Mail Removed)> escribió en el mensaje
news:%(E-Mail Removed)...
> "Harvey Triana" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 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...?
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      27th Jul 2006
Harvey Triana <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Harvey Triana
Guest
Posts: n/a
 
      27th Jul 2006
Excelent. Thanks.

<Harvey Triana />

"Jon Skeet [C# MVP]" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed)...
> Harvey Triana <(E-Mail Removed)> wrote:
>> 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 - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
page shifted left, no left blue margin, no default on top Erich D. Microsoft Word Document Management 1 4th Feb 2010 05:30 PM
I'm left handed How to move scrollbar to left side of a window? =?Utf-8?B?VGhvbWFzRQ==?= Windows XP Accessibility 1 6th Nov 2007 06:04 AM
How to make a cell appear in upper left (top left) corner of works =?Utf-8?B?amVmZg==?= Microsoft Excel Programming 2 6th Mar 2007 10:14 PM
Left-to-right slide sorter view -- convert to right-to-left order =?Utf-8?B?SEdpbG1hbkJlbm5ldHQ=?= Microsoft Powerpoint 6 15th Jun 2006 04:39 PM
How do I insert a symbol to left of left margin so text lines up? =?Utf-8?B?Y2phY2tzb24=?= Microsoft Word Document Management 5 13th Mar 2006 10:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:41 PM.