To replace characters except first and last characterJithil John11yAsked Oct 22, 2015, 11:16 PM EDTSolvedShareLinkedInXCopy linkSaveTo replace characters except first and last character from a word in .net?
To replace characters except first and last characterJithil John11yAsked Oct 22, 2015, 11:16 PM EDTSolvedShareLinkedInXCopy linkSaveTo replace characters except first and last character from a word in .net?
Francis SusaimichaelRank #466 · 5.4K reputation11y agoPosted Oct 23, 2015, 12:19 AM EDT✓ Accepted answer0Try the below code: int i; String strMyText = "This is an Simple Example"; string[] arr = strMyText.Split(' '); foreach(string str in arr) { int iFirstIndex = str.IndexOf(str[0]); int iLastIndex = str.LastIndexOf(str[str.Length-1]); string temp = ""; for (i = 0; i < str.Length ; i++) { if (str.Length > 2) if (i != iFirstIndex && i != iLastIndex) temp += "*"; else temp += str[i]; } Console.WriteLine(temp); } Output:T**sS****eE*****e Hope this helps! 0Sreekanth ReddyRank #726 · 3.2K reputation11y agoPosted Oct 22, 2015, 11:30 PM EDT0can you provide a small example, what you needed? 0Jithil JohnRank #2,791 · 506 reputation11y agoPosted Oct 22, 2015, 11:41 PM EDT0T****E H***G D**F0Jithil JohnRank #2,791 · 506 reputation11y agoPosted Oct 22, 2015, 11:41 PM EDT0T****E H***G D**F0Priyaranjan K SRank #45 · 32.5K reputation11y agoPosted Oct 23, 2015, 12:07 AM EDT0Hi Jithil , You may try out the code below : string originalString ="I cannot"; string replacementString="can"; var aStringBuilder = new System.Text.StringBuilder(originalString); int lastCharacterIndex=originalString.Length; aStringBuilder.Remove(1,lastCharacterIndex-2); aStringBuilder.Insert(1,replacementString); Console.WriteLine(aStringBuilder); Output : Icant Thanks,Priyan If you found this useful Please Accept this as Answer 0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sreekanth ReddyRank #726 · 3.2K reputation11y agoPosted Oct 22, 2015, 11:30 PM EDT0can you provide a small example, what you needed? 0
Priyaranjan K SRank #45 · 32.5K reputation11y agoPosted Oct 23, 2015, 12:07 AM EDT0Hi Jithil , You may try out the code below : string originalString ="I cannot"; string replacementString="can"; var aStringBuilder = new System.Text.StringBuilder(originalString); int lastCharacterIndex=originalString.Length; aStringBuilder.Remove(1,lastCharacterIndex-2); aStringBuilder.Insert(1,replacementString); Console.WriteLine(aStringBuilder); Output : Icant Thanks,Priyan If you found this useful Please Accept this as Answer 0
Francis SusaimichaelRank #466 · 5.4K reputationPosted Oct 23, 2015, 12:19 AM EDT
Sreekanth ReddyRank #726 · 3.2K reputationPosted Oct 22, 2015, 11:30 PM EDT
Jithil JohnRank #2,791 · 506 reputationPosted Oct 22, 2015, 11:41 PM EDT
Jithil JohnRank #2,791 · 506 reputationPosted Oct 22, 2015, 11:41 PM EDT
Priyaranjan K SRank #45 · 32.5K reputationPosted Oct 23, 2015, 12:07 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts