How to get the second word of a string in C#

3 Answers

0 votes
using System;

class GetTheSecondWordOfString_CSharp
{
    static void Main()
    {
        string str = "c java c# python c++ rust";
        string[] words = str.Split(' ');

        if (words.Length > 1) {
            string secondWord = words[1];
            Console.WriteLine("The second word is: " + secondWord);
        }
        else {
            Console.WriteLine("The input string does not contain enough words.");
        }
    }
}


/*
run:

The second word is: java

*/

 



answered Oct 3, 2024 by avibootz
edited Oct 3, 2024 by avibootz
0 votes
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string str = "c java c# python c++ rust";
        
        string secondWord = str.Split(' ').Skip(1).FirstOrDefault();
        
        Console.WriteLine(secondWord);
    }
}


/*
run:

java

*/

 



answered Oct 3, 2024 by avibootz
0 votes
using System;

class Program
{
    static void Main()
    {
        string str = "c java c# python c++ rust";
        
        string secondWord = str.Split(' ')[1];
        
        Console.WriteLine(secondWord);
    }
}


/*
run:

java

*/

 



answered Oct 3, 2024 by avibootz

Related questions

1 answer 107 views
1 answer 125 views
1 answer 172 views
1 answer 148 views
1 answer 169 views
1 answer 177 views
1 answer 116 views
...