Day 6 of 30-Day .NET Challenge: String built-in Methods
Tags: webdev, beginners, csharp, dotnet
Published: 2024-03-25 06:04:34

## Introduction

The module demonstrates string helper methods to pinpoint and extract the desired information.

### Learning Objectives

* Locate the position of a character or substring within another string

* Retrieve segments of strings

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

### IndexOf method

Utilize the IndexOf() method to find the position of a single or multiple characters/strings within a larger string.

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

    public static class StringMethods
    {
        /// <summary>
        /// Outputs
        /// 13
        /// 36
        /// </summary>
        public static void IndexOfExample()
        {
            string message = "Find what is (inside the parentheses)";
    
            int openingPosition = message.IndexOf('(');
            int closingPosition = message.IndexOf(')');
    
            Console.WriteLine(openingPosition);
            Console.WriteLine(closingPosition);
        }
    }

Execute the code from the main method as follows

    #region Day 6 - String built-in methods
    
    StringMethods.IndexOfExample();
    
    #endregion

### Console Output

    13
    36

### Substring method

Use the Substring() method to extract the part of the main string that comes after the specified character positions.

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

    /// <summary>
    /// Outputs
    /// (inside the parentheses
    /// </summary>
    public static void SubstringExample()
    {
        string message = "Find what is (inside the parentheses)";
    
        int openingPosition = message.IndexOf('(');
        int closingPosition = message.IndexOf(')');
    
        int length = closingPosition - openingPosition;
        Console.WriteLine(message.Substring(openingPosition, length));
    }

Execute the code from the main method as follows

    #region Day 6 - String built-in methods
    
    StringMethods.SubstringExample();
    
    #endregion

### Console Output

    (inside the parentheses

### Skip the first character “(”

Simply update the starting index position using openingPosition += 1;To do that add another method into the same static class as shown below
```
    /// <summary>
    /// Outputs
    /// inside the parentheses
    /// </summary>
    public static void SubstringExample2()
    {
        string message = "Find what is (inside the parentheses)";
    
        int openingPosition = message.IndexOf('(');
        int closingPosition = message.IndexOf(')');
    
        openingPosition += 1;
    
        int length = closingPosition - openingPosition;
        Console.WriteLine(message.Substring(openingPosition, length));
    }
```
Execute the code from the main method as follows
```
    #region Day 6 - String built-in methods
    
    StringMethods.SubstringExample2();
    
    #endregion
```
### Console Output
```
    inside the 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)