是否可以用字符串替换特定位置的字符
让我们说有一个字符串: "I am a man"
我想用字符串替换7处的字符"wom"(不管原始字符是什么).
最终结果应该是: "I am a woman"
a=new String("Hello");
Run Code Online (Sandbox Code Playgroud)

a[0]==="H" //true
a[0]="J"
a[0]==="J" //false
a[0]==="H" //true
Run Code Online (Sandbox Code Playgroud)
这是否意味着我只能使用字符串作为char的数组.split("")然后.join("")呢?
答案:是的,在Javascript strings are readonly(又名不变)这个问题的答案如下:
a="12345"
a[2]=3
a[2]='9'
console.log(a) //=> "12345"
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事??这个怪癖给我带来了1个小时的痛苦调试.如何以合理的方式避免这种情况?
var dataUrl = $('#AjaxMoreStatus').attr("data-url");
// http://localhost:8888/app/status/get/31/7
Run Code Online (Sandbox Code Playgroud)
我需要将最后一个破折号(7)的链接的最后一个值更改为另一个值.7是动态值.
我可以做到吗?
例如,如果我想将动态值7改为9:
var new = 9;
$("#AjaxMoreStatus").attr("data-url", url without the 7 + '/' + new);
Run Code Online (Sandbox Code Playgroud) 我试图在javascript中大写字符串中的字符,我的代码是:
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = "string";
for(m = 0; m < str.length; m++){
if(str[m] == "r"){
str[m+1] = str[m+1].toUpperCase();
}
}
alert(str);
}
</script>
Run Code Online (Sandbox Code Playgroud)
所以我想要做的是,如果角色是r,则大写下一个角色.但是不是woking意味着它是相同的字符串警报.
所以我试图做一个".replace"循环,但是发生了一些神秘的事情.
var cell = "r1c1";
for (i = 0; i <= 4; i++){
cell = cell.replace(cell[3],i+1);
Run Code Online (Sandbox Code Playgroud)
我的预期产量:
cell = "r1c1"
cell = "r1c2"
cell = "r1c3"
cell = "r1c4"
cell = "r1c5"
Run Code Online (Sandbox Code Playgroud)
实际输出:
cell = "r1c2"
cell = "r2c1"
cell = "r2c3"
cell = "r2c4"
cell = "r2c5"
Run Code Online (Sandbox Code Playgroud)
如您所见,它在第二次迭代时运行正常的EXCEPT.世界上我做错了什么?
我们如何通过索引查找并用字符串替换它?(参见下面的预期产出)
我尝试了下面的代码,但它也替换了下一个字符串。(来自这些堆栈)
String.prototype.replaceAt = function(index, replacement) {
return this.substr(0, index) + replacement + this.substr(index + replacement.length);
}
var hello = "hello world";
console.log(hello.replaceAt(2, "!!")); // He!!o World
Run Code Online (Sandbox Code Playgroud)
预期输出:
var toReplace = "!!";
1. "Hello World" ==> .replaceAt(5, toReplace) ===> "Hello!!World"
2. "Hello World" ==> .replaceAt(2, toReplace) ===> "He!!lo World"
Run Code Online (Sandbox Code Playgroud)
注意: toReplace变量可以是动态的。
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Run Code Online (Sandbox Code Playgroud)
是否有可能用来.replace改变amet上面句子中的所有内容?
我有一个字符串,并且必须使用数组中的开始/结束值在字符串中插入“ div”。
var str = "this is a simple string";
var places = [{
"start": 0,
"end": 3
},
{
"start": 9,
"end": 15
}];
function insert(places, str) {
// I tryed different approaches like this:
str = str.forEach(function(item, index) {
var start = item.start;
var end = item.end;
var newStr = '<div>'+str[start] + '</div>' + str.slice(end);
str = newStr;
});
$('.h').html(str);
// I need to get output str as:
// "<div>this</div> is a <div>simple</div> string"
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div …Run Code Online (Sandbox Code Playgroud)有人可以解释这种行为吗
代码:
name = "ABC"
console.log(name)
name[0] = "B"
console.log(name)
Run Code Online (Sandbox Code Playgroud)
预期输出:
ABC
BBC
Run Code Online (Sandbox Code Playgroud)
如果我有字符串"hello"并且我想用_替换第二个和第三个字符,我怎么能这样做,只给出子字符串的位置,而不是它实际上是什么.
function reverse1(str){
var a = "";
for(var i = 0; i <= str.length/2; i++){
a = str[i];
str[i] = str[str.length-i-1];
str[str.length-i-1] = a;
}
return str;
}
var str = "abcdef";
reverse1(str);
Run Code Online (Sandbox Code Playgroud)
我想在不使用任何内置函数的情况下反转字符串,并且希望它更改原始字符串本身,但效果不佳。语言是Javascript。
我尝试创建一个程序,用我自己的规则将文本加密成代码.但是,我发现分裂文本并将其发送到数组有一些问题
我想在Msg数组中看到这个
Msg[0] = "h";
Msg[1] = "e";
Msg[2] = "l";
Msg[3] = "l";
Msg[4] = "o";
Run Code Online (Sandbox Code Playgroud)
我尝试像这样编码
String text = "hello";
String[] Msg = new String[] {text};
Run Code Online (Sandbox Code Playgroud)
这段代码有效,但结果并不像我想要的那样.你能帮我解决这个问题吗?
谢谢..
javascript ×12
string ×4
jquery ×2
replace ×2
actionscript ×1
arrays ×1
html ×1
java ×1
readonly ×1
substring ×1
typescript ×1
uppercase ×1