Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

csharp method word first and last letter

Tags:

c#

i have method, which have parameter "word" returns Word first and the last letter with string. between first and last letter there is three points. example when you write "stackoverflow" it returns it like that "s...w"

I have this code but i wont work.

namespace stackoverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            string word = "stackoverflow";
            string firsLast = FirsLast(word);
            Console.WriteLine(firsLast);
            Console.ReadKey();
        }

        private static string FirsLast(string word)
        {
            string firsLast = "...";
            for (int i = 0; i < word.Length; i += 2)
            {
                firsLast += word.ElementAt(i);
            }
            return firsLast;
        }
    }
}
like image 868
coodienoobie Avatar asked Dec 09 '22 04:12

coodienoobie


2 Answers

Why not

if (word.Length >= 2)
{
               return word[0] + "..." + word[word.Length - 1];
}
like image 190
drhanlau Avatar answered Dec 29 '22 02:12

drhanlau


if (word.Length >= 2)
{
    return word.First() + "..." + word.Last();
}
like image 40
Phil Lambert Avatar answered Dec 29 '22 02:12

Phil Lambert