w3resource

C#: Check if "HP" appears at second position in a string and returns the string without "HP"

C# Sharp Basic: Exercise-37 with Solution

Write a C# program to check if "HP" appears at the second position in a string and return the string without "HP".

C# Sharp Exercises: Check if 'HP' appears at second position in a string and returns the string without 'HP'

Sample Solution:-

C# Sharp Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Exercise37 {
    static void Main(string[] args) {
        // Define a string variable 'str' and assign it the value "PHP Tutorial"
        string str = "PHP Tutorial";

        // Check if the substring from index 1 of length 2 in 'str' equals "HP"
        // If true, remove the substring "HP" starting from index 1; otherwise, keep the original string 'str'
        Console.WriteLine((str.Substring(1, 2).Equals("HP") ? str.Remove(1, 2) : str));
    }
}

Sample Output:

P Tutorial

Flowchart:

Flowchart: C# Sharp Exercises - Check if 'HP' appears at second position in a string and returns the string without 'HP'.

C# Sharp Code Editor:

Previous: Write a C# program to check if an integer (from the two given integers) is in the range -10 to 10.
Next: Write a C# program to get a new string of two characters from a given string. The first and second character of the given string must be "P" and "H", so PHP will be "PH".

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/basic/csharp-basic-exercise-37.php