编写一个函数,该函数接受字符串输入并删除所有出现的大于或等于给定数字的字符。
RemoveCharacters("Spanish", 2)
应该返回“ panih”
RemoveCharacters("Spanish", 3)
应该返回“西班牙语”
string text = "Spanish";
var sb = new StringBuilder(text.Length);
int maxCount = 2;
int currentCount = 2;
char specialChar = 'S';
foreach (char c in text)
if (c != specialChar || ++currentCount <= maxCount)
sb.Append(c);
text = sb.ToString();
int commasFound = 0;
int maxCommas = 1;
text = new string(text.Where(c => c != 'S' || ++commasFound <= maxCommas).ToArray());
Console.WriteLine(text);
答案 0 :(得分:1)
让我们以两个步骤处理字符串:
$em_category = $this->getDoctrine()->getManager();
$em_category->getRepository('AppBundle:Category')->findOneBy(array('id' => ($request->get('id_category'))));
$userInterest->setCategory($em_category);
$em = $this->getDoctrine()->getManager();
$em->persist($userInterest);
$em->flush();
次)不通行
{
"username": "Usertest",
"password": "strenghtpassword",
"email": "thxforall@symfony.com",
"birth": "1999-04-26T18:25:43-05:00",
"content": "I like Netflix",
"id_category": "1"
}
一些测试:
count
结果:
private static String RemoveCharacters(string value, int count) {
if (string.IsNullOrEmpty(value))
return value;
else if (count <= 1)
return "";
HashSet<char> toRemove = new HashSet<char>(value
.GroupBy(c => char.ToUpper(c))
.Where(chunk => chunk.Count() >= count)
.Select(chunk => chunk.Key));
return string.Concat(value.Where(c => !toRemove.Contains(char.ToUpper(c))));
}
答案 1 :(得分:0)
我不是在为您写作业,而是考虑使用字典。迭代单词中的字符。如果字典中存在该元素,则增加该元素。否则,将其插入到您的词典中,其值为1。然后迭代您的词典键,并注意哪些键超出了您的目标。最后,写出不包含这些字符的字符串。