Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 删除最后一个字母后的所有字符_C#_Visual Studio 2010 - Fatal编程技术网

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

C# 删除最后一个字母后的所有字符,c#,visual-studio-2010,C#,Visual Studio 2010,下面的简单程序将查找用户输入的字符串中的最后一个字母,然后删除该点之后的所有内容。因此,如果一个人输入一个字符串….则g之后的所有内容都应删除。我将以下内容作为一个小程序: 类程序 { 静态void Main(字符串[]参数) { Write(“输入字符串的值:”); List charList=Console.ReadLine().Trim().ToList(); intx=charList.LastIndexOf(charList.Last(char.isleter)); WriteLine

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

类程序
{
静态void Main(字符串[]参数)
{
Write(“输入字符串的值:”);
List charList=Console.ReadLine().Trim().ToList();
intx=charList.LastIndexOf(charList.Last(char.isleter));
WriteLine(“这是最后一个字母{0}”,x);
WriteLine(“这是字符串{0}”的长度,charList.Count);
WriteLine(“我们应该删除最后的{0}个字符”,charList.Count-x);
for(int i=x;i
我已经尝试了很多不同的方法,但没有一个能完全做到。这个输入为
string….
的特殊程序的输出是
strin….
,所以不知何故,它留下了应该带走的内容,实际上带走了不应该带走的字母。有人能说明为什么会发生这种情况吗?所需的输出也应该是
string

尝试以下操作:

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

我认为简单地使用用户输入的
Substring
string
更为直接。因此,考虑下面修改的代码:

类程序
{
静态void Main(字符串[]参数)
{
Write(“输入字符串的值:”);
var s=Console.ReadLine().Trim();
List charList=s.ToList();
intx=charList.LastIndexOf(charList.Last(char.isleter));
WriteLine(“这是最后一个字母{0}”,x);
WriteLine(“这是字符串{0}”的长度,charList.Count);
WriteLine(“我们应该删除最后的{0}个字符”,charList.Count-x);
控制台写入线(s.Substring(0,x+1);
Console.ReadLine();
}
}

在这里,我们存储用户输入的值,找到字母的最后一个索引,然后在向控制台写入时通过该字母找到子字符串。

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

Jsut解释一下,这是因为e每次删除一个字符时,您都会增加i计数器,但也会减少字符列表。计数,因此您实际上删除了一个字符,保留了下一个字符,然后再次删除,依此类推

例如,当输入“string…”且x为5(G字母的索引)时,您正在执行以下操作:

第1次迭代: 删除g字符,使x变为6,字符列表。计数变为9(10-1)

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

所以你错过了第一点


我允许您检查其他答案,因为它们包含针对您的问题的更优雅的解决方案。

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


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

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


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


您可以使用此扩展:

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


解决方案将是这样的

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

为什么不直接使用
input.Substring(0,x+1)
?@Grant Thomas:因为如果x不存在并且所有符号都被删除了,那么x可能是-1,也许这就是目的,从“规范”中还不太清楚。@MichelKeijzers这是不相关的。关键是所有的循环都是一场闹剧-值的验证是微不足道的。你知道程序现在的运行方式吗?当r我想它应该是x+1而不是x.+1不需要
子字符串
字符串.Join(“,input.TakeWhile(char.isleter))
这将失败!这将不会查找最后一个字母,它将查找第一个非字母并删除其余的字母,包括后面的字母。是的,这将在
“ABC.ABC”上失败。
这将(根据规范)产生
“ABC.ABC”
@MartinMulder/MatthewWatson没有我最初的解决方案那么优雅,但它更好地满足了要求。我不得不说,这是一个比我更优雅的解决方案。+1这将导致搜索两次字符数组。这就是我一直在寻找的。感谢这一见解,我相信这将帮助我完成任务mewhere。这里有一个相关的线程:这将失败!这不会查找最后一个字母,它将查找第一个非字母,并删除其余的,包括后面的字母。@MartinMulder感谢您指出,您是非常正确的。现在做了一点更改,所以我相信它现在应该是正确的。很好……但是……请注意:这是一个错误《nswer》已经由马修·沃森(Matthew Watson)提交了。巧合,那又怎样?我最初的答案是在这里,并做了一点小改动。这很重要。(此外,我的答案在最初和编辑后都更可读;))如果它如此低效,为什么要发布它?@MartinMulder“只是为了好玩”。可能你不是那种人!但它确实展示了Linq在反转序列方面的一些用途,这可能会引起一些兴趣。
string s = console.ReadLine();
s = s.Substring(0, s.ToList().FindLastIndex(char.IsLetter) + 1);
var trimmedInput = string.Join("", input.Reverse().SkipWhile(x => !char.IsLetter(x)).Reverse());
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);
}
string charList = "string..."; //any string place here
int x = charList.LastIndexOf(charList.Last(char.IsLetter));
String str = charList.ToString().Substring(0, x + 1);