[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

EASY, Peezy Points!!!! How can I do a substring like this is C#?

Hi all,

I am moving some code from one of my SP's to my app code and I am not quite sure how to accomplish the same thing in C#. here is what I am trying to do ("Get ONLY the first name (and pref. omit any trailing initials) from an alphaname field which is in the format [lastname], [firstname] [optional initial]

Here is my T-SQL (but it doesn't omit initals):

SUBSTRING(sr.AlphaName, CHARINDEX(',', sr.AlphaName) + 1, len(sr.AlphaName)) As 'First Name'


Any takers???


Thanks to all who can offer some wisdom!!!
0
OddiC
Asked:
OddiC
4 Solutions
 
digital_thoughtsCommented:

VariableName.Substring(test.IndexOf(",") + 1, VariableName.Length)
0
 
StanicDejanCommented:
   public string FirstName(string s)
    {
      string result = s.Substring(s.IndexOf(',') + 1).Trim();
      if (result.IndexOf(' ') >= 0)
        result = result.Substring(0, result.IndexOf(' '));
      return result;
    }

HTH,
Dejan
0
 
burakiewiczCommented:
this will work if the fisrtname doest have a blank space
string strVariable = "Burakiewicz,Nathan M";
            int i = strVariable.IndexOf(',');
            string strFirstName = strVariable.Substring(i + 1, strVariable.Length - (i + 1));
            strFirstName=strFirstName.Remove(strFirstName.LastIndexOf(' '));
0
 
brejkCommented:
string x = "Smith, John";
x = x.Substring(x.IndexOf(",") + 1).Trim();
0
 
OddiCAuthor Commented:
Thanks to all- great help!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now