I have written a c# code to read string values within brackets “()”
string s = "Hello (World) This is (Working)";
int i = 0;
while ((i = s.IndexOf('(', i)) != -1)
{
int stop = s.Substring(i+1).IndexOf(')');
string output = s.Substring(i + 1, stop);
Console.WriteLine(output);
i++;
}
Console.ReadLine();
with this code I get
World Working
I want to implement this in JavaScript So I tried this
function myFunction()
{
var s = "Hello (World) This is (Working)";
var i = 0;
while ((i = s.indexOf('(', i)) != -1)
{
var stop = s.substring(i+1).indexOf(')');
var output = s.substring(i+1, stop);
document.write(output);
i++;
}
}
but It has different output
ss (
I am not good at javaScript, Please help me find a solution in this method I dont want to use regex
Despite similar names, these aren’t actually using equivalent methods (note the 2nd arguments):
Substring(int index, int length)substring(int indexA, int indexB)Try
substrinstead for the JavaScript —substr(int index, int length):