DEV Community

Cover image for Day 7 of 30-Day .NET Challenge: String built-in Methods Part 2
Sukhpinder Singh
Sukhpinder Singh

Posted on • Updated on • Originally published at singhsukhpinder.Medium

Day 7 of 30-Day .NET Challenge: String built-in Methods Part 2

Introduction

The article demonstrates the IndexOfAny() method to locate the initial occurrence of any string from a chosen array. Additionally, you utilize LastIndexOf() to pinpoint the last occurrence of a string within another string.

Learning Objectives

  • Learn to use LastIndexOf() method

  • Learn to use IndexOfAny() method

Prerequisites for Developers

  • Basic familiarity with string helper methods

  • Basic understanding of while iteration statements

  • Proficiency in using Visual Studio or Visual Studio Code for C# code development, building, and execution

Getting Started

LastIndexOf Method

To enhance the complexity of the “message” variable by incorporating numerous sets of parentheses, followed by coding to extract the content enclosed within the last set of parentheses.

To begin, create a static class file called “StringMethodsPart2.cs” within the console application. Insert the provided code snippet into this file.

    /// <summary>
    /// Outputs
    /// set of parentheses
    /// </summary>
    public static void LastIndexOfMethod() {

        string message = "(What if) I am (only interested) in the last (set of parentheses)?";
        int openingPosition = message.LastIndexOf('(');

        openingPosition += 1;
        int closingPosition = message.LastIndexOf(')');
        int length = closingPosition - openingPosition;
        Console.WriteLine(message.Substring(openingPosition, length));
    }
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

    #region Day 7 - String built-in methods Part 2

    StringMethodsPart2.LastIndexOfMethod();

    #endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

Searching THIS message: Help (find) the {opening symbols}
Found WITHOUT using startPosition: (find) the {opening symbols}
Enter fullscreen mode Exit fullscreen mode

IndexOfAny Method

Utilize .IndexOfAny() to retrieve the index of the initial symbol from the openSymbols array that is present in the message string.

To do that add another method into the same static class as shown below


    public static class StringMethodsPart2
    {
        /// <summary>
        /// Outputs
        /// Searching THIS message: Help (find) the {opening symbols}
        /// Found WITHOUT using startPosition: (find) the { opening symbols }
        /// </summary>
        public static void IndexOfAnyMethod()
        {
            string message = "Help (find) the {opening symbols}";
            Console.WriteLine($"Searching THIS Message: {message}");
            char[] openSymbols = ['[', '{', '('];

            int openingPosition = message.IndexOfAny(openSymbols);
            Console.WriteLine($"Found WITHOUT using startPosition: {message.Substring(openingPosition)}");

        }
    }
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

    #region Day 7 - String built-in methods Part 2

    StringMethodsPart2.IndexOfAnyMethod();

    #endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

    set of parentheses
Enter fullscreen mode Exit fullscreen mode

Complete Code on GitHub

GitHub — ssukhpinder/30DayChallenge.Net

C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

If you’ve made it this far, please show your appreciation with a clap and follow the author! 👏️️

Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr

Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev

More content at C# Programming

Top comments (2)

Collapse
 
beekrum profile image
Bikram Duwal • Edited

Hi, Sukhpinder Singh

This is a great article series for beginners. I am in love with this C# series of article.
In the above article the code snippets are mismatched, LastIndexOf Method has IndexOfAny Method code snippets and vice-versa.

Collapse
 
ssukhpinder profile image
Sukhpinder Singh

Thanks @beekrum i have corrected those out