Frank Myat Thu
Frank Myat Thu

Reputation: 4474

single quote to double quote , and , double quote to single quote in javascript

I want to change

every single quote to double quote and every double quote to single quote

in jquery or js.

var myString = " tes't tes\"t tes\"t te'st ";
console.log(myString);

for (i = 0; i < myString.length; i++) {
    console.log(myString[i]);
    if (myString[i] == "'") {
        myString[i] == "\"";
            continue;
    }
    if (myString[i] == "\"") {
        myString[i] == "'";
            continue;
    }
}

console.log(myString);

But my function is not working well. Please correct me.

Upvotes: 2

Views: 832

Answers (5)

Salman Arshad
Salman Arshad

Reputation: 272106

Besides the incorrect use of == operator, you have another problem in your code:

myString[i] can be used to get the ith character of the string (in most browsers); but it cannot be used to "change" that character. This will work:

var a = myString[i];

This won't:

myString[i] = a;

Here is your code, re-written with minor changes plus the use of charAt and substring to read and change characters of string:

var myString = " tes't tes\"t tes\"t te'st ";
console.log(myString);
for (i = 0; i < myString.length; i++) {
    if (myString.charAt(i) == "'") {
        myString = myString.substring(0, i) + '"' + myString.substring(i + 1);
    } else if (myString.charAt(i) == '"') {
        myString = myString.substring(0, i) + "'" + myString.substring(i + 1);
    }
}
console.log(myString);

Upvotes: 1

BGerrissen
BGerrissen

Reputation: 21680

First, strings are immutable, any operation on strings like .replace() actually returns a new string. You can NOT do the following:

myString[ i ] = "x";

Your other mistake was using a comparison operator as assignment operator.

myString[ i ] == "x";

Knowing this, you need to construct a new string and replace the value of myString with the new value. One way of constructing a string is using the .split('') method to create an array representation of the string, perform mutations on the array and then revert back to string using the .join('') method of the array.

var myString = " tes't tes\"t tes\"t te'st ";
var tokens = myString.split('');

for (i = 0; i < tokens.length; i++) {
  if (tokens[i] == "'") {
    tokens[i] = "\"";
    continue;
  }
  if (tokens[i] == "\"") {
    tokens[i] = "'";
    continue;
  }
}
myString = tokens.join('');
console.log(myString);

Upvotes: 1

mornaner
mornaner

Reputation: 2424

You are doing comparations, not assingments. Also, you can't change a string that way, you need to create a new one:

var myString = " tes't tes\"t tes\"t te'st ";
    console.log(myString);
    var str = [];

    for (i = 0; i < myString.length; i++) {
        console.log(myString[i]);
        if (myString[i] == "'") {
            str.push("\"");
        }
        else if (myString[i] == "\"") {
            str.push("'");
            continue;
        }
       else
      {
        str.push(myString[i]);
      }
    }
    str.join("");

    console.log(str);

Check it in this live demo

Upvotes: 2

Minko Gechev
Minko Gechev

Reputation: 25682

Here is one more solution using different approach:

var myString = " tes't t%es\"t tes\"t te'st ",
result = myString.replace(/%/g, '\\%')
.replace(/'/g, '%')
.replace(/"/g, "'")
.replace(/[^\\]%/g, function (m) {
   return m[0] + '"';
})
.replace('\\%', '%');

Upvotes: 1

Yoshi
Yoshi

Reputation: 54649

Inside the ifs you're not doing an assignment (myString[i] == "\"";) but a comparsion. This should probably be a single =. Only problem is, strings are immutable and thus this would not work as expected. Solution, build a new string:

function flipQuotes(str) {
  var result = [], i = 0, lim = str.length, c;

  for (; i < lim; i += 1) {
    c = str.charAt(i);
    switch(c) {
      case '"':
        result.push("'");
        break;

      case "'":
        result.push('"');
        break;

      default:
        result.push(c);
    }
  }

  return result.join('');
}

var myString = " tes't tes\"t tes\"t te'st ";
console.log(
  flipQuotes(myString) // => ' tes"t tes't tes't te"st '
);

Upvotes: 3

Related Questions