Solved

Left Function in C#

Posted on 2007-04-02
5
889 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 142

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 142

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now