Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all characters after the last letter

The following simple program will find the last letter in a string that a user enters and then remove everything after that point. So, if a person enters one string.... everything after the g should be removed. I've got the following as a little program:

class Program
{
    static void Main(string[] args)
    {
        Console.Write("Enter in the value of the string: ");
        List<char> charList = Console.ReadLine().Trim().ToList();

        int x = charList.LastIndexOf(charList.Last(char.IsLetter)) ;
        Console.WriteLine("this is the last letter {0}", x);
        Console.WriteLine("This is the length of the string {0}", charList.Count);
        Console.WriteLine("We should have the last {0} characters removed", charList.Count - x);

        for (int i = x; i < charList.Count; i++)
        {
            charList.Remove(charList[i]);
        }

        foreach (char c in charList)
        {
            Console.Write(c);
        }
        Console.ReadLine();
    }
}

I've tried numerous variations of this and none of them getting it exactly write. This particular program with an input of string.... the output of the program is strin.. So somehow it's leaving on what it should be taking away and it's actually taking away letters that it shouldn't. Can anyone give an indication as to why this is happening? The desired output, again should be string.

like image 928
wootscootinboogie Avatar asked Apr 29 '13 12:04

wootscootinboogie


People also ask

How do I remove all characters from a string after a specific character?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

How do you delete after last occurrence of a character?

The indexOf method returns the index of the first occurrence of a character in the string. If you need to remove everything after the last occurrence of a specific character, use the lastIndexOf method.

How do you delete everything after the last in Python?

The addition (+) operator can be used to concatenate strings in Python. What is this? If you need to remove everything after the last occurrence of the character in the string, use the str. rsplit() method.

How do you remove the text before or after a specific character in Excel?

To eliminate text before a given character, type the character preceded by an asterisk (*char). To remove text after a certain character, type the character followed by an asterisk (char*). To delete a substring between two characters, type an asterisk surrounded by 2 characters (char*char).


1 Answers

Try this:

string input = Console.ReadLine();                // ABC.ABC.
int index = input.Select((c, i) => new { c, i })
                 .Where(x => char.IsLetter(x.c))
                 .Max(x => x.i);
string trimmedInput = input.Substring(0, index + 1);
Console.WriteLine(trimmedInput);                  // ABC.ABC
like image 185
p.s.w.g Avatar answered Sep 28 '22 11:09

p.s.w.g