Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,542 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,859 people online right now. Registration is fast and FREE... Join Now!




Get leftmost character

 
Reply to this topicStart new topic

Get leftmost character

andiyuniar
21 Aug, 2008 - 11:50 PM
Post #1

New D.I.C Head
*

Joined: 15 Apr, 2007
Posts: 35


My Contributions
hi there...

i want to get the leftmost character from string.
i use this code

CODE

using Microsoft.VisualBasic;


tgllhr = Left(dr[2].ToString(),10);



but it did'nt work...

Any idea... how to solve this?


thanks
User is offlineProfile CardPM
+Quote Post

n8wxs
RE: Get Leftmost Character
22 Aug, 2008 - 03:34 AM
Post #2

D.I.C Regular
***

Joined: 6 Jan, 2008
Posts: 456



Thanked: 43 times
My Contributions
QUOTE(andiyuniar @ 22 Aug, 2008 - 12:50 AM) *

hi there...

i want to get the leftmost character from string.
i use this code

CODE

using Microsoft.VisualBasic;


tgllhr = Left(dr[2].ToString(),10);



but it did'nt work...

Any idea... how to solve this?


thanks


You may need to fully qualify the Left() method call if you are using it in a
class (form) that also has a Left property:

csharp

tgllhr = Microsoft.VisualBasic.Strings.Left(dr[2].ToString(),10);


The Left() method does not throw errors. The user is supposed to use the Len()
function to be sure the Left() method will return the desired substring. So if your
dr[2].ToString() is returning a null string Left() won't complain.

Another way to do what you want is to use the Substring() method of the System
namespace's String Class:

csharp

using System;

string tgllhr;

try
{
tgllhr = dr[2].ToString().Substring(0,10);
}
catch (ArgumentOutOfRangeException e) {
Console.WriteLine(e.Message);
}



User is online!Profile CardPM
+Quote Post

zakary
RE: Get Leftmost Character
22 Aug, 2008 - 06:05 AM
Post #3

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 405



Thanked: 6 times
Dream Kudos: 175
My Contributions
n8wxs is on the right path with the substring() in c# we can set any part of a string by using the
String.Substring(int startIndex, int length) method. Here is an example

csharp

string left = dr[2].ToString().Substring(0,5);


or you can use the String.Remove(int startIndex, int count);

csharp

string left =dr[2].ToString().Remove(5, dr[2].ToString().Length);


either way you will get the left or first few characters of the string
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Get Leftmost Character
22 Aug, 2008 - 07:56 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
If you just want the left most (first chanracter) use

csharp

string left = dr[2].ToString().Substring(0,1);


That will return the first character in the string. Now if you always need the last character try using

csharp

string left = dr[2].ToString().Substring((dr[2].ToString().Length - 1),dr[2].Length);

User is online!Profile CardPM
+Quote Post

eclipsed4utoo
RE: Get Leftmost Character
22 Aug, 2008 - 06:41 PM
Post #5

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 363



Thanked: 19 times
Dream Kudos: 25
My Contributions
QUOTE(PsychoCoder @ 22 Aug, 2008 - 11:56 AM) *


Now if you always need the last character try using

csharp

string left = dr[2].ToString().Substring((dr[2].ToString().Length - 1),dr[2].Length);



FYI, that code throws an error...

QUOTE
Index and length must refer to a location within the string.


I think you meant this to always get the last character

csharp

string left = dr[2].ToString().Substring((dr[2].ToString().Length - 1), 1);


User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Get Leftmost Character
22 Aug, 2008 - 10:33 PM
Post #6

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
This also works

Usage:
CODE

            MessageBox.Show(GetLeftChar("Hippo"));


Code:
CODE

        private String GetLeftChar(String text)
        {
            String myText = text.Remove(1, text.Length - 1);
            return myText;
        }


if you want to get the Right Character then use this

Usage:
CODE

            MessageBox.Show(GetRightChar("Hippo"));


Code:
CODE

        private String GetRightChar(String text)
        {
            String myText = text.Remove(0, text.Length - 1);
            return myText;
        }


This post has been edited by gbertoli3: 22 Aug, 2008 - 10:34 PM
User is online!Profile CardPM
+Quote Post

andiyuniar
RE: Get Leftmost Character
24 Aug, 2008 - 06:58 PM
Post #7

New D.I.C Head
*

Joined: 15 Apr, 2007
Posts: 35


My Contributions
thanks... it's very helpfull
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Get Leftmost Character
24 Aug, 2008 - 07:14 PM
Post #8

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
Always Happy to Help!
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:44PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month