我必须在一个包含40多个字母的句子中拆分这个词......
**For Example:**
Hi Pranesh....I am new to C# asp.Net...**pranuvideotitlepranuvideotitlepranutitleppranuvideotitlepranuvideotitlepranutitlep**
我想找到包含超过40个字母的单词,并希望删除单词中的字母,使其仅包含40个字母....
由于
答案 0 :(得分:2)
首先找到包含超过40个字母的所有单词
var a = content.Split(' ').Where(s => s.Length > 40);
然后通过forech循环删除
答案 1 :(得分:1)
我喜欢扩展方法!
我会做这样的事情:
public static string Truncate(this string s, int maxLength) {
if (string.IsNullOrEmpty(s) || maxLength <= 0)
return string.Empty;
else if (s.Length > maxLength)
return s.Substring(0, maxLength) + "…";
else
return s;
}
编辑:问题不是很清楚。如果你想删除少于40个字符的所有单词,你可以这样:
public string FindBigWords(string s) {
Regex regEx = new Regex(@"\s+");
string[] tokens = regEx.Split(s);
string ret = "";
foreach (var t in tokens) {
if (t.Length > 40)
ret += t;
}
return ret;
}
注意我没有测试过上面的内容,效率不高。可能想要改变它至少使用stringbuilder。
答案 2 :(得分:1)
这样的东西?
string data = @"Hi Pranesh....I am new to C# asp.Net...**pranuvideotitlepranuvideotitlepranutitleppranuvideotitlepranuvideotitlepranutitlep**";
string[] split = Regex.Split(data, @"\s");
foreach(string word in split)
{
if (word.Length > 40)
{
data = data.Replace(word, word.Substring(0, 40));
}
}
答案 3 :(得分:1)
怎么样
static string FourtyLetterWords(string s)
{
var splitString = GetWords(s);
return string.Join("", splitString.Select(u => u.Count() >= 40 ? u.Substring(0, 40) : u));
}
private static List<string> GetWords(string s)
{
var stringList = new List<string>();
StringBuilder currentWord = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
if(char.IsLetter(s[i]))
{
currentWord.Append(s[i]);
}
else
{
stringList.Add(currentWord.ToString());
currentWord.Clear();
stringList.Add(s[i].ToString());
}
}
return stringList;
}
使用 -
调用string test = FourtyLetterWords(@"Hi Pranesh....I am new to C# asp.Net...**pranuvideotitlepranuvideotitlepranutitleppranuvideotitlepranuvideotitlepranutitlep**");
返回 - “你好Pranesh ......我是C#asp.Net的新手...... pranuvideotitlepranuvideotitlepranutitle ”
答案 4 :(得分:0)
public static string TruncateLongWords(this string sentence, int maximumWordLength)
{
var longWords = sentence.Split(' ').Where(w => w.Length > maximumWordLength);
foreach (var longWord in longWords)
{
sentence = sentence.Replace(longWord, longWord.Substring(maximumWordLength));
}
return sentence;
}
答案 5 :(得分:0)
var a = content.Split('')。Where(s =&gt; s.Length&lt; = 40);
这将给出截断的内容。您可以在var对象上使用ToArray()来获取字符串数组。