What's the StringBuilder equivalent in TypeScript?
13,437
In Typescript, for string replacement, use RegExp
function changePercentage(input) {
return input.replace(/%/, "#");
}
console.log(changePercentage("AHu%odk"));
Author by
Harold_Finch
I love problem solving using algorithms and that's that. Not biased to any language.
Updated on June 06, 2022Comments
-
Harold_Finch about 2 years
So I have this string that contains a percentage symbol
%
. I want to be able to replace it with the#
symbol usingTypeScript
. I am still learning TypeScript so it's syntax is still throwing me off. I managed to implement the solution that I need in C# and that piece of code can be found below:public static void Main(string[] args) { string data = "AHu%odk"; //The % character will always be at index 3 string finalResult = ""; if (data.Contains("%")) { StringBuilder sbFirstIndex = new StringBuilder(data); sbFirstIndex[3] = '#'; finalResult = sbFirstIndex.ToString(); Console.WriteLine("Appended string now looks like this: {0} \n", finalResult); } else { Console.WriteLine("False. It does not contain it"); } }
Here is my TypeScript implementation but I got stuck if checking for the index of the character:
changePercantage(input: string): void { var data= "AHu%odk"; var finalResult = ""; var index = result.indexOf("%"); if (index == 3) { //Replace the percentage with the # character } }
The version of TypeScript that I am using is
v2.3.3.0
.Angular :
v5.0.0
Node:
v8.9.3
Npm:
v5.6.0
-
Harold_Finch over 6 yearsYou come to my rescue again in the same day! Thank you :-)
-
Abhijit Kar ツ over 6 years@Harold_Finch, my pleasure!