Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,044 questions

55,901 answers

573 users

How to get the last word from string in C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        string s = "vb.net javascript php c c++ python c#";
        string lastWord = GetLastWord(s);
        Console.WriteLine("1. " + lastWord);
        
        lastWord = GetLastWord("");
        Console.WriteLine("2. " + lastWord);
 
        lastWord = GetLastWord("c#");
        Console.WriteLine("3. " + lastWord);
 
        lastWord = GetLastWord("c c++ java ");
        Console.WriteLine("4. " + lastWord);
 
        lastWord = GetLastWord("  ");
        Console.WriteLine("5. " + lastWord);
    }

    static string GetLastWord(string input) {
        if (string.IsNullOrWhiteSpace(input))
            return "";

        input = input.Trim(); // remove leading/trailing spaces

        int pos = input.LastIndexOf(" ");
        return pos > -1 ? input.Substring(pos + 1) : input;
    }
}


  
/*
run:
  
1. c#
2. 
3. c#
4. java
5. 
  
*/

 



answered Sep 8, 2019 by avibootz
edited Mar 27 by avibootz
...