Solved

Left Function in C#

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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
Code works but breaks when I add one section 4 27
Adding where clause to entity statement c# 1 46
C# XML Get Values 4 33
Stop Git from being my repository 1 16
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

740 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