如何在javascript中拆分时避免使用空格 [英] how to avoid white spaces while splitting in javascript

查看:50
本文介绍了如何在javascript中拆分时避免使用空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串为1234 | 5678 | 2345 | Hello World | 43456 | 45345 | 5345

当我将此字符串拆分为|角色



它会返回

1234

5678

2345
你好



但我希望它为



1234

5678

2345

Hello World

43456

45345

5345

i have string as "1234|5678|2345|Hello World|43456|45345|5345"
when i split this string with | character

it will return
1234
5678
2345
Hello

but i want it as

1234
5678
2345
Hello World
43456
45345
5345

推荐答案

检查您是否在 split limit 参数值c>限制拆分次数的功能



Check whether you are using limit parameter value in split function to limit the number of splits

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = "1234|5678|2345|Hello World|43456|45345|5345";
    var res = str.split("|").join("<br/>");
    document.getElementById("demo").innerHTML = res;
}
</script>





OutPut:

1234

5678

2345

Hello World

43456

45345

5345



OutPut:
1234
5678
2345
Hello World
43456
45345
5345


以下工作正常:

The following works fine :
var s = "1111|2222|hello there|99999";
var o = s.split("|");
console.log(o);



你得到:


and you get :

["1111", "2222", "hello there", "99999"] 


这篇关于如何在javascript中拆分时避免使用空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆