删除最后一个字母后的所有字符

时间:2013-04-29 12:53:57

标签: c# visual-studio-2010

以下简单程序将找到用户输入的字符串中的最后一个字母,然后删除该点之后的所有内容。因此,如果一个人在string....被删除后输入一个g。我作为一个小程序得到了以下内容:

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();
    }
}

我尝试了很多这方面的变化,但没有一个能让它完全写出来。这个特殊的程序输入string....程序的输出是strin..所以不知何故它会留下它应该带走的东西,它实际上是在拿掉它本不应该的字母。任何人都可以说明为什么会这样吗?所需的输出也应该是string

10 个答案:

答案 0 :(得分:5)

试试这个:

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

答案 1 :(得分:3)

Jsut的解释,那是因为每次删除一个字符时,你都会递增i计数器,但也会减少charList.Count,所以你实际上删除了1个字符,留下了下一个字符,然后再删除等等。

例如,输入“string ....”并且x为5(G字母的索引),你正在做:

第一次迭代: 删除g char,使x变为6,charList.Count变为9(10-1)

下一次迭代: 删除索引6处的char,现在是第二个。 (你的字符串是“strin。 ..”)。

所以你错过了第一点。

我让你检查其他答案,因为它们包含更优雅的解决方案。

答案 2 :(得分:2)

我认为仅仅Substring用户输入的string就更直接了。因此,请考虑以下修改后的代码:

 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();
    }
}

这里我们将用户输入的值存储到s中,找到一个字母的最后一个索引,然后在写到控制台时通过该字母Substring

答案 3 :(得分:2)

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

答案 4 :(得分:1)

您还可以使用名为SubString的字符串函数来获取从第一个字母到最后一个字母索引的所有内容。

答案 5 :(得分:1)

这是一种非常低效的方法(只是为了好玩!)

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

答案 6 :(得分:1)

您可以使用此扩展程序:

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);
}

输入:test...abc... 输出:test...abc

DEMO

答案 7 :(得分:1)

解决方案就是这样。

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

答案 8 :(得分:0)

这将匹配每个单词字符(A-Z,0-9和_):

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

答案 9 :(得分:0)

我认为这是最短,最简单的选择:

编辑: 评论指出了一个初始错误,所以我添加了一些修复。现在应该很好地工作(可能不是最佳解决方案,但我认为这是一个有趣的简单解决方案):

var userInput = Console.ReadLine();

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