2

I have applied the same method to replace "-" with "_" in c++ and it is working properly but in javascript, it is not replacing at all.

function replace(str)
{
    for(var i=0;i<str.length;i++)
    {
        if(str.charAt(i)=="-")
        {
            str.charAt(i) = "_";
        }
    }
    return str;
}
Ankur
  • 457
  • 6
  • 21
  • Javascript strings are immutable. You cannot change individual character indicies. A regular expression would be much easier than a `for` loop, as well. – CertainPerformance Jul 08 '18 at 04:53
  • Possible duplicate of [How do I replace a character at a particular index in JavaScript?](https://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript) –  Jul 08 '18 at 05:59

3 Answers3

4

It's simple enough in javascript, it's not really worth making a new function:

function replace(str){
  return str.replace(/-/g, '_') // uses regular expression with 'g' flag for 'global'
}

console.log(replace("hello-this-is-a-string"))

This does not alter the original string, however, because strings in javascript are immutable.

If you are dead set on avoiding the builtin(maybe you want to do more complex processing), reduce() can be useful:

function replace(str){
  return [...str].reduce((s, letter) => s += letter == '-' ? '_' : letter , '')
}

console.log(replace("hello-this-is-a-string"))
Mark
  • 90,562
  • 7
  • 108
  • 148
1

You can easily replace complete string using .split() and .join().

function replace(str){
  return str.split("-").join("_");
}

console.log(replace("hello-this-is-a-string"))
Karan
  • 12,059
  • 3
  • 24
  • 40
  • 1
    There's no need to use `split` and `join`, and in fact it's slower (and less semantic). Any performance improvement over `replace` was eliminated ten years ago. In today's browsers, `replace` is probably twice as fast. See https://jsperf.com/split-join-vs-replace. –  Jul 08 '18 at 05:39
1

This is yet another case of "make sure you read the error message". In the case of

str.charAt(i) = "_";

the correct description of what happens is not "it is not replacing at all", as you would have it; it is "it generates a run-time JavaScript error", which in Chrome is worded as

Uncaught ReferenceError: Invalid left-hand side in assignment

In Firefox, it is

ReferenceError: invalid assignment left-hand side

That should have given you the clue you needed to track down your problem.

I repeat: read error messages closely. Then read them again, and again. In the great majority of cases, they tell you exactly what the problem is (if you only take the time to try to understand them), or at least point you in right direction.

Of course, reading the error message assumes you know how to view the error message. Do you? In most browsers, a development tools window is available--often via F12--which contains a "console", displaying error messages. If you don't know about devtools, or the console, then make sure you understand them before you write a single line of code.

Or, you could have simply searched on Stack Overflow, since (almost) all questions have already been asked there, especially those from newcomers. For example, I searched in Google for

can i use charat in javascript to replace a character in a string?

and the first result was How do I replace a character at a particular index in JavaScript?, which has over 400 upvotes, as does the first and accepted answer, which reads:

In JavaScript, strings are immutable, which means the best you can do is create a new string with the changed content, and assign the variable to point to it.

As you learn to program, or learn a new languages, you will inevitably run into things you don't know, or things that confuse you. What to do? Posting to Stack Overflow is almost always the worst alternative. After all, as you know, it's not a tutorial site, and it's not a help site, and it's not a forum. It's a Q&A site for interesting programming questions.

In the best case, you'll get snarky comments and answers which will ruin your day; in the worst case, you'll get down-voted, and close-voted, which is not just embarrassing, but may actually prevent you from asking questions in the future. Since you want to make sure you are able to ask questions when you really need to, you are best off taking much more time doing research, including Google and SO searches, on simple beginner questions before posting. Or find a forum which is designed to help new folks. Or ask the person next to you if there is one. Or run through one or more tutorials.

But why write it yourself at all?

However, unless you are working on this problem as a way of teaching yourself JavaScript, as a kind of training exercise, there is no reason to write it at all. It has already been written hundreds, or probably thousands, of times in the history of computing. And the overwhelming majority of those implementations are going to be better, faster, cleaner, less buggy, and more featureful than whatever you will write. So your job as a "programmer" is not to write something that converts dashes to underscores; it's to find and use something that does.

As the wise man said, today we don't write algorithms any more; we string together API calls. Our job is to find, and understand, the APIs to call.

Finding the API is not at all hard with Google. In this case, it could be helpful if you knew that strings with underscores are sometimes called "snake-cased", but even without knowing that you can find something on the first page of Google results with a query such as "javascript convert string to use underscores library".

The very first result, and the one you should take a look at, is underscore.string, a collection of string utilities written in the spirit of the versatile "underscore" library, and designed to be used with it. It provides an API called underscored. In addition to dealing with "dasherized" input (your case), it also handles other string/identifier formats such as "camelCase".

Even if you don't want to import this particular library and use it (which you probably should), you would be much better off stealing or cloning its code, which in simplified form is:

str
  .replace(/([a-z\d])([A-Z]+)/g, '$1_$2')
  .replace(/[-\s]+/g, '_')

This is not as complicated as it looks. The first part is to deal with camelCase input, so that "catsAndDogs" becomes "cats-and-dogs". the second line is what handles the dashes and spaces). Look closely--it replaces runs of multiple dashes with a single underscore. That could easily be something that you want to do too, depending on who is doing what with the transformed string downstream. That's a perfect example of something that someone else writing a professional-level library for this task has thought of that you might not when writing your home-grown solution.

Note that this well-regarded, finely-turned library does not use the split/join trick to do global replaces in strings, as another answer suggests. That approach went out of use almost a decade ago.

Besides saving yourself the trouble of writing it yourself, and ending up with better code, if you take time time to understand what it's doing, you will also end up knowing more about regular expressions.