loviji
loviji

Reputation: 13080

Remove characters before character "."

How effectively remove all character in string that placed before character "."?

Input: Amerika.USA

Output: USA

Upvotes: 79

Views: 172646

Answers (8)

sɐunıɔןɐqɐp
sɐunıɔןɐqɐp

Reputation: 3512

I use the following extension methods for cropping everything before or after a given character token.

I also have the same methods for string token, but I won't post them here to avoid cluttering.

PS: The cropping of the token can be controlled with the optional paramater.

public static string CropAfterFirst(this string str, char searchToken, bool cropSearchTokenToo = true)
{
    int index = str?.IndexOf(searchToken) ?? -1;
    return index < 0 ? str : str.Substring(0, cropSearchTokenToo ? index : ++index);
}

public static string CropBeforeFirst(this string str, char searchToken, bool cropSearchTokenToo = true)
{
    int index = str?.IndexOf(searchToken) ?? -1;
    return index < 0 ? str : str.Substring(cropSearchTokenToo ? ++index : index);
}

public static string CropAfterLast(this string str, char searchToken, bool cropSearchTokenToo = true)
{
    int index = str?.LastIndexOf(searchToken) ?? -1;
    return index < 0 ? str : str.Substring(0, cropSearchTokenToo ? index : ++index);
}

public static string CropBeforeLast(this string str, char searchToken, bool cropSearchTokenToo = true)
{
    int index = str?.LastIndexOf(searchToken) ?? -1;
    return index < 0 ? str : str.Substring(cropSearchTokenToo ? ++index : index);
}

The answer for the OP's question is:

// Removes everything before the last dot, including the dot itself:
string output = input.CropBeforeLast('.');

// Removes everything before the first dot, including the dot itself:
string output = input.CropBeforeFirst('.'); 

Upvotes: 0

Ben
Ben

Reputation: 2020

Extension methods I commonly use to solve this problem:

public static string RemoveAfter(this string value, string character)
    {
        int index = value.IndexOf(character);
        if (index > 0)
        {
            value = value.Substring(0, index);
        }
        return value;
    }

    public static string RemoveBefore(this string value, string character)
    {
        int index = value.IndexOf(character);
        if (index > 0)
        {
            value = value.Substring(index + 1);
        }
        return value;
    }

Upvotes: 6

luke
luke

Reputation: 14788

String input = ....;
int index = input.IndexOf('.');
if(index >= 0)
{
    return input.Substring(index + 1);
}

This will return the new word.

Upvotes: 6

Niko Zarzani
Niko Zarzani

Reputation: 1402

A couple of methods that, if the char does not exists, return the original string.

This one cuts the string after the first occurrence of the pivot:

public static string truncateStringAfterChar(string input, char pivot){         
    int index = input.IndexOf(pivot);   
    if(index >= 0) {
        return input.Substring(index + 1);          
    }           
    return input;       
}

This one instead cuts the string after the last occurrence of the pivot:

public static string truncateStringAfterLastChar(string input, char pivot){         
    return input.Split(pivot).Last();   
}

Upvotes: 3

casperOne
casperOne

Reputation: 74530

You can use the IndexOf method and the Substring method like so:

string output = input.Substring(input.IndexOf('.') + 1);

The above doesn't have error handling, so if a period doesn't exist in the input string, it will present problems.

Upvotes: 167

Christian
Christian

Reputation: 321

You could try this:

string input = "lala.bla";
output = input.Split('.').Last();

Upvotes: 32

ryudice
ryudice

Reputation: 37366

public string RemoveCharactersBeforeDot(string s)
{
 string splitted=s.Split('.');
 return splitted[splitted.Length-1]
}

Upvotes: 4

Itay Karo
Itay Karo

Reputation: 18286

string input = "America.USA"
string output = input.Substring(input.IndexOf('.') + 1);

Upvotes: 12

Related Questions