user3258052
user3258052

Reputation: 59

Trim a string in c# after special character

I want to trim a string after a special character..

Lets say the string is str="arjunmenon.uking". I want to get the characters after the . and ignore the rest. I.e the resultant string must be restr="uking".

Upvotes: 3

Views: 6407

Answers (9)

Anil Mahajan
Anil Mahajan

Reputation: 214

you can use like

string input = "arjunmenon.uking";
int index = input.LastIndexOf(".");
input = input.Substring(index+1, input.Split('.')[1].ToString().Length  );

Upvotes: 1

odpro
odpro

Reputation: 86

I think the simplest way will be this:

string restr, str = "arjunmenon.uking";
restr = str.Substring(str.LastIndexOf('.') + 1);

Upvotes: 0

Sebastien H.
Sebastien H.

Reputation: 7126

Not like the methods that uses indexes, this one will allow you not to use the empty string verifications, and the presence of your special caracter, and will not raise exceptions when having empty strings or string that doesn't contain the special caracter:

string str = "arjunmenon.uking";
string restr = str.Split('.').Last();

You may find all the info you need here : http://msdn.microsoft.com/fr-fr/library/b873y76a(v=vs.110).aspx

cheers

Upvotes: 0

Raja Danish
Raja Danish

Reputation: 235

string str = "arjunmenon.uking";
string[] splitStr = str.Split('.');
string restr = splitStr[1];

Upvotes: 0

Adrian Salazar
Adrian Salazar

Reputation: 5319

Personally, I won't do the split and go for the index[1] in the resulting array, if you already know that your correct stuff is in index[1] in the splitted string, then why don't you just declare a constant with the value you wanted to "extract"?

After you make a Split, just get the last item in the array.

string separator = ".";
string text = "my.string.is.evil";
string[] parts = text.Split(separator);

string restr = parts[parts.length - 1];

The variable restr will be = "evil"

Upvotes: 0

quake
quake

Reputation: 9

Try Regular Expression Language

using System.IO;
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "arjunmenon.uking";
        string pattern = @"[a-zA-Z0-9].*\.([a-zA-Z0-9].*)";

        foreach (Match match in Regex.Matches(input, pattern))
       {
         Console.WriteLine(match.Value);
         if (match.Groups.Count > 1)
            for (int ctr = 1; ctr < match.Groups.Count; ctr++) 
               Console.WriteLine("   Group {0}: {1}", ctr, match.Groups[ctr].Value);
      }

    }
}

Result:
arjunmenon.uking
    Group 1: uking

Upvotes: 0

Wasif Hossain
Wasif Hossain

Reputation: 3940

char special = '.';

var restr = str.Substring(str.IndexOf(special) + 1).Trim();

Upvotes: 0

Rohan
Rohan

Reputation: 703

Use Split function Try this

string[] restr = str.Split('.');
//restr[0] contains arjunmenon
//restr[1] contains uking

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062520

How about:

string foo = str.EverythingAfter('.');

using:

public static string EverythingAfter(this string value, char c)
{
    if(string.IsNullOrEmpty(value)) return value;
    int idx = value.IndexOf(c);
    return idx < 0 ? "" : value.Substring(idx + 1);
}

Upvotes: 7

Related Questions