Delete all characters after the last letter

The following simple program will find the last letter in the line that the user enters, and then delete everything after this point. So, if a person enters one string.... everything after deleting g . I have the following 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 tried many variations of this, and none of them got it exactly. This particular program with the input string.... displays the strin.. program strin.. So for some reason it takes up what it should take away, and it actually takes away letters that it shouldn't. Can anyone indicate why this is happening? The desired result should again be string .

+6
source share
10 answers

I think it would be much easier to just enter the Substring the string user. Therefore, consider the following modified code:

  class Program { static void Main(string[] args) { Console.Write("Enter in the value of the string: "); var s = Console.ReadLine().Trim(); List<char> charList = s.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); Console.WriteLine(s.Substring(0, x + 1); Console.ReadLine(); } } 

here we save the value entered by the user in s , find the last index of the letter, and then Substring through this letter when writing to the console.

+2
source

Try the following:

 string input = Console.ReadLine(); // ABC.ABC. int index = input.Select((c, i) => new { c, i }) .Where(x => char.IsLetter(xc)) .Max(x => xi); string trimmedInput = input.Substring(0, index + 1); Console.WriteLine(trimmedInput); // ABC.ABC 
+5
source

Jsut for an explanation, because every time you delete a character, you increase the counter i, but also decrease charList.Count, so you actually delete 1 character, leaving the next one, then deleting it again, etc.

For example, when you enter "string ...." and x is 5 (the index of the letter G), which you do:

1st iteration: Remove g char so x becomes 6 and charList.Count becomes 9 (10-1)

Next iteration: Remove the char with index 6, which is now second. (your string is "strin ... ...").

So, you missed the first point.

I let you check other answers as they contain more elegant solutions to your problems.

+3
source
 string s = console.ReadLine(); s = s.Substring(0, s.ToList().FindLastIndex(char.IsLetter) + 1); 
+2
source

You can also use a string function called SubString to get everything from the first to the last index of letters.

+1
source

Here's a pretty inefficient way to do this (just for fun!)

 var trimmedInput = string.Join("", input.Reverse().SkipWhile(x => !char.IsLetter(x)).Reverse()); 
+1
source

You can use this extension:

 public static string TrimLettersLeft(this string input) { int lastLetterIndex = -1; for (int i = input.Length - 1; i >= 0; i--) { if (Char.IsLetter(input[i])) { lastLetterIndex = i; break; } } if( lastLetterIndex == -1) return input; else return input.Substring(0, lastLetterIndex + 1); } 

Input: test...abc... Output: test...abc

Demo

+1
source

The solution will be like this.

 string charList = "string..."; //any string place here int x = charList.LastIndexOf(charList.Last(char.IsLetter)); String str = charList.ToString().Substring(0, x + 1); 
+1
source

This will correspond to each character of the word (AZ, 0-9 and _):

 string Input = Console.ReadLine(); string Userinput = String.Empty; Regex TextSearch = new Regex(@"\w*"); if(TextSearch.IsMatch(Input)) Userinput = TextSearch.Match(Input).Groups[0].Value; else // No valid Input 
0
source

I believe this is the shortest and easiest option:

Edit: The comment indicates the initial error here, so I added a small fix. Should work well now (maybe this is not the optimal solution, but I thought it was a fun solution anyway):

 var userInput = Console.ReadLine(); Console.WriteLine(new string(userInput.Reverse() .SkipWhile(c => !char.IsLetter(c)) .Reverse() .ToArray())); 
0
source

Source: https://habr.com/ru/post/943880/


All Articles