473,387 Members | 1,394 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,387 software developers and data experts.

"Left" Function Equivalent in C#

Greetings,

I have a "newbie" question. I was wondering if there is anything in C#
that corresponds to VB's "Left" function? I simply want to retrieve the
leftmost characters of a string and I wasn't sure what the best way was
to do that.

Thanks in advance!

*** Sent via Developersdex http://www.developersdex.com ***
Jun 20 '06 #1
3 75356
OutdoorGuy wrote:
Greetings,

I have a "newbie" question. I was wondering if there is anything in C#
that corresponds to VB's "Left" function? I simply want to retrieve the
leftmost characters of a string and I wasn't sure what the best way was
to do that.


string s = "12345";
string left_three_of_s = s.Substring(0, 3);
Jun 20 '06 #2
How about substring:

int i = 3;
string x = "abcdefg";

string y = x.Substring( 0, i );

y will equal the first 3 characters of x

Martin

"OutdoorGuy" <Ou********@fishing.com> wrote in message
news:uT**************@TK2MSFTNGP02.phx.gbl...
Greetings,

I have a "newbie" question. I was wondering if there is anything in C#
that corresponds to VB's "Left" function? I simply want to retrieve the
leftmost characters of a string and I wasn't sure what the best way was
to do that.

Thanks in advance!

*** Sent via Developersdex http://www.developersdex.com ***

Jun 20 '06 #3

"OutdoorGuy" <Ou********@fishing.com> wrote in message
news:uT**************@TK2MSFTNGP02.phx.gbl...
Greetings,

I have a "newbie" question. I was wondering if there is anything in C#
that corresponds to VB's "Left" function? I simply want to retrieve the
leftmost characters of a string and I wasn't sure what the best way was
to do that.

Thanks in advance!

*** Sent via Developersdex http://www.developersdex.com ***


In addition to the other replies, and using their substring answer to create
a utility method that performs the Left (although you can use the
VisualBasic reference and import it...)

public string Left(string Original, int Count)
{
// Can't remember if the Left function throws an exception in this case,
but for
// this method, we will just return the original string.
if (Original == null || Original == string.Empty || Original.Length <
Count) {
return Original;
} else {
// Return a sub-string of the original string, starting at index 0.
return Original.Substring(0, Count);
}
}

public string Right(string Original, int Count)
{
// same thing as above.
if (Original == null || Original == string.Empty || Original.Length <
Count) {
return Original;
} else {
// blah blah blah
return Original.Substring(Original.Length - Count);
}
}

btw, this is untested code :)

HTH,
Mythran

Jun 20 '06 #4

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

Similar topics

3
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def,...
3
by: davidkarlsson74 | last post by:
Error: document.getElementById("folderMenu").cells has no properties File: http://www.volkswagen.se/tillbehor/js/foldermenu.js Rad: 49 The function activates different DIV:s, but doesn't seem to...
2
by: Tasman | last post by:
Is there an equivalent of valign="bottom" for a table within a DIV using CSS? I've found I can center a table of buttons in a DIV with {margin-left:auto; margin-right:auto}. But I am unsure how...
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
2
by: Angus | last post by:
I am trying to change the selection in Javascript - but this HTML element is not a standard option control. On the web page it looks like a dropdown list - and you click on the right hand down...
1
by: dhtmlkitchen | last post by:
Why does "foo" instanceof String return false? It just seems counterintuitive to me. I mean, sure, I can always check the constructor: var fooVar = "foo"; var isString =...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.