删除最后一个字母之后的所有字符
本文关键字:字符 之后 最后一个 删除 | 更新日期: 2023-09-27 18:11:25
下面的简单程序将查找用户输入的字符串中的最后一个字母,然后删除该点之后的所有内容。因此,如果一个人进入了一个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
。
试试这个:
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
只是为了解释,这是因为每次删除一个字符时,i计数器会增加,但charList也会减少。计数,所以你实际上删除了一个字符,留下下一个,然后再次删除,等等。
例如,输入"string...."并且x为5 (G字母的索引),您正在做的是:
第一次迭代:删除g字符,使x变成6和charList。Count变为9 (10-1)
下一次迭代:删除索引6处的字符,现在是第二个。(你的字符串是" string ....").
所以你错过了第一点。
我让你检查其他答案,因为它们包含更优雅的解决方案。
我认为将用户输入的string
简化为Substring
会更直接。因此,考虑以下修改后的代码:
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
。
string s = console.ReadLine();
s = s.Substring(0, s.ToList().FindLastIndex(char.IsLetter) + 1);
您还可以使用一个名为SubString的字符串函数来获取从第一个字母到最后一个字母的所有索引
这是一种非常低效的方法(只是为了好玩!)
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);
}
输入: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);
这将匹配每个单词字符(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
我认为最短、最简单的选择是:
编辑: 一个注释指出了这里最初的错误,所以我添加了一个小修复。现在应该可以很好地工作了(可能不是最佳解决方案,但我认为这是一个有趣而简单的解决方案):
var userInput = Console.ReadLine();
Console.WriteLine(new string(userInput.Reverse()
.SkipWhile(c => !char.IsLetter(c))
.Reverse()
.ToArray()));