开发者

How to split the words in a sentence which has more than 40 letters in asp.net

I have to split the word in a sentence which contains more than 40 letters....

   **For Example:**

    Hi Pranesh....I am new to C# asp.Net...**pranuvideotitlepranuvideotitlepranutitleppranuvideotitlepranuvideotitlepranutitlep**

I want to find the word which contains more than 40 letters and want to de开发者_高级运维lete the letters from the word so that it contains only 40letters....

Thanks


first find all word which contains more than 40 letters

var a = content.Split(' ').Where(s => s.Length > 40);

then remove by forech loop


I love extension methods!

I would do something like this:

 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) + "&hellip;";
        else
            return s;
 }

EDIT: The question wasn't very clear. If you want to strip out all words less than 40 characters you could so something like:

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

Note I haven't tested the above and it's not very efficient. Probably want to change it to at least use a stringbuilder.


Something like this?

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


How about

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

Called using -

string test = FourtyLetterWords(@"Hi Pranesh....I am new to C# asp.Net...**pranuvideotitlepranuvideotitlepranutitleppranuvideotitlepranuvideotitlepranutitlep**");

Which returns - "Hi Pranesh....I am new to C# asp.Net...pranuvideotitlepranuvideotitlepranutitle"


var a = content.Split(' ').Where(s => s.Length <= 40);

This will give the truncated content. You can use ToArray() on the var object to get a string array.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜