Solved

Left Function in C#

Posted on 2007-04-02
5
899 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

628 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