How can we check if a string 'Ra' appears at index 1 in a given string. If it appears return a string without 'Ra' otherwise return the original string.
Sample Input:
"Ram"
"Rat"
"Coding"
Expected Output:
Code
m
t
CodingHow can we check if a string 'Ra' appears at index 1 in a given string. If it appears return a string without 'Ra' otherwise return the original string.
Sample Input:
"Ram"
"Rat"
"Coding"
Expected Output:
m
t
CodingKnow the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Nov 14, 2022, 9:30 AM EST
Try
string text = "Coding"; if(text.Substring(0, 2) == "Ra") Console.WriteLine(text.Remove(0, 2)); else Console.WriteLine(text);this
Sachin SinghRank #66 · 26.2K reputationPosted Nov 14, 2022, 9:46 AM EST
Have not tested but it should work, please test this
public string GetTexts(string str) { var regex=new Regex(@"^\bRa(\w+)\b"); if(regex.IsMatch(str)) { Match m=regex.Match(str); return m.Groups[1].ToString(); } else { return str; } }