kkiranonly Posted January 27, 2006 Share Posted January 27, 2006 Hi,This is KiranKumar and new to this forum.Recently i faced a problem like,I have a String like:var str = "Sony DCR30 PC CAMERA";in JavaScript.So,i want to replace all empty strings in str with "_";I used like :str.replace(\ \,"_");//But it's get affected only for the first space.I mean like this : Sony_DCR30And not for rest of all,and not even displaying the remaining string.So,i require your help in this regards.Is there any other function like(replaceAll() in java) to resolve this issue.Looking forward to recieve an Immediate Response.Thanks for spending your valuable time for me.......RegardsKiran Kumar.K Link to comment Share on other sites More sharing options...
kkiranonly Posted January 27, 2006 Author Share Posted January 27, 2006 Hi,We can resolve it by just taking that string into a loop Use loop likefor(int i=0;i<str.length();i++){str = str.replace(\ \,"_");} Link to comment Share on other sites More sharing options...
aspnetguy Posted January 27, 2006 Share Posted January 27, 2006 a more efficient way would be like this. var loopProtect = 0; //protect against infinite loopswhile(str.indexOf(" ") != -1 && loopProtect < str.length){ str = str.replace(\ \,"_"); loopProtect++;} With your code you were looping for the length of the string even if all the spaces were already gone.The code I provided will only loop while there are still spaces present. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now