w3resource

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


Remove 'HP' from String

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'.

For more Practice: Solve these Related Problems:

  • Write a C# program to remove any occurrence of "HP" anywhere in a string.
  • Write a C# program to remove all uppercase letter pairs from a string.
  • Write a C# program to replace "HP" with "HighPerformance" in a string only if it starts at position 1.
  • Write a C# program to detect if the string starts with 'P' but the previous character was 'H' and remove both.

Go to:


PREV : Check Integer in Range -10 to 10.
NEXT : Extract 'PH' from String.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.