How to use LastIndexOf() to search a string from the right (reverse) in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "c# c c++ java";

            int i = s.LastIndexOf('c');
            if (i != -1)
            {
                Console.WriteLine(i);
                Console.WriteLine(s.Substring(i));
            }
        }
    }
}


/*
run:

5
c++ java

*/

 



answered Mar 7, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "c# c c++ java";

            int i = s.LastIndexOf("C", StringComparison.OrdinalIgnoreCase);
            if (i != -1)
            {
                Console.WriteLine(i);
                Console.WriteLine(s.Substring(i));
            }
        }
    }
}


/*
run:

5
c++ java

*/

 



answered Mar 7, 2017 by avibootz

Related questions

...