remove first and last character from string c# code example

Example 1: c# remove last character from string

string Name = "Teste,"
string ReturnName = "";
ReturnName = Name.Remove(Name.Length - 1);

Example 2: remove first character in a string c#

// initial string is "/temp" it will be changed to "temp"
data.Remove(0,1);
data.TrimStart('/');
data.Substring(1);

Example 3: remove last instance of string c#

public static string ReplaceLastOccurrence(string Source, string Find, string Replace)
{
        int place = Source.LastIndexOf(Find);

        if(place == -1)
           return Source;

        string result = Source.Remove(place, Find.Length).Insert(place, Replace);
        return result;
}

Tags: