简体   繁体   English

双引号的单引号,以及javascript中单引号的双引号

[英]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. 在jquery或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. 请指正。

Inside the if s 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: if s中你没有做一个赋值( myString[i] == "\\""; )但是比较。这应该是单个= 。唯一的问题是,字符串是不可变的,因此这不会起作用预期。解决方案,构建一个新的字符串:

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 '
);

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 这个现场演示中查看它

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); myString[i]可用于获取字符串的第i个字符(在大多数浏览器中); 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: 这是你的代码,重写了一些小改动,加上使用charAtsubstring来读取和更改字符串的字符:

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);

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('\\%', '%');

First, strings are immutable, any operation on strings like .replace() actually returns a new string. 首先,字符串是不可变的,对字符串的任何操作.replace().replace()实际上都返回一个新字符串。 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. 知道了这一点,你需要构造一个新的字符串并用新值替换myString的值。 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. 构造字符串的一种方法是使用.split('')方法创建字符串的数组表示,在数组上执行突变,然后使用数组的.join('')方法恢复为字符串。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM