Day 7 of 30-Day .NET Challenge: String built-in Methods Part 2
Tags: webdev, beginners, csharp, programming
Published: 2024-03-26 05:02:45



## 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.
```csharp
    /// <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));
    }
```
Execute the code from the main method as follows
```csharp
    #region Day 7 - String built-in methods Part 2
    
    StringMethodsPart2.LastIndexOfMethod();
    
    #endregion
```
### Console Output
    Searching THIS message: Help (find) the {opening symbols}
    Found WITHOUT using startPosition: (find) the {opening symbols}


### 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
```csharp

    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)}");
    
        }
    }
```
Execute the code from the main method as follows
```csharp
    #region Day 7 - String built-in methods Part 2
    
    StringMethodsPart2.IndexOfAnyMethod();
    
    #endregion
```
### Console Output
```csharp
    set of parentheses
```

### Complete Code on GitHub
[GitHub — ssukhpinder/30DayChallenge.Net](https://github.com/ssukhpinder/30DayChallenge.Net/tree/main)

### 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](https://twitter.com/sukhsukhpinder) | [LinkedIn](https://www.linkedin.com/in/sukhpinder-singh/) | [Dev.to](https://dev.to/ssukhpinder) | [Hashnode](https://dotnet.hashnode.dev/) | [Newsletter](https://www.linkedin.com/newsletters/net-programming-7031098498754195456/) | [Tumblr](https://www.tumblr.com/settings/blog/codewithsukh)

Visit our other platforms: [GitHub](https://github.com/ssukhpinder) | [Instagram](https://www.instagram.com/codewithsukh/) | [Tiktok](https://www.tiktok.com/@codewithsukh) | [Quora](https://www.quora.com/profile/Sukhpinder-Singh-4) | [Daily.dev](https://app.daily.dev/devcard)

More content at [C# Programming](https://medium.com/c-sharp-progarmming)