Solved

Left Function in C#

Posted on 2007-04-02
5
894 Views
Last Modified: 2008-02-01
Hello experts,

How do I get Left of something from a string in C#
I have something like this:

string str="ABC_1234";

Now how do I do something like str=Left(str,"_");
I just want ABC form the str.

Pls help
0
Comment
Question by:rowmark
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18838402
str.substring(1, str.Find("_") -1 );
0
 

Author Comment

by:rowmark
ID: 18838502
"Find" is not there in the string properties.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18838520
sorry, it's called indexof. as that function is zero-based, it will be written like this:

str.Substring(1, str.IndexOf(@"_") );
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 18838522
try using the index of method, this will return the index from zero at which the character/string you wish to find is located or will return -1 if not present
0
 
LVL 8

Expert Comment

by:Rytmis
ID: 18838572
           string Left(string input, string delimiter) {
                  int delimIndex = input.IndexOf(delimiter);
                  if(delimIndex < 0)
                        return "";
                  
                  return input.Substring(0, delimIndex);
            }

Off the top of my hat :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SqlDependency to get update from sql to my c# app 2 63
How to call weather service by using asmx? 4 44
Cant save 3D 4 22
VB.NET 2008 Winforms Signing 13 31
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question