Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Left Function in C#

Posted on 2007-04-02
5
Medium Priority
?
906 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 2000 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

Technology Partners: 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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 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