我有以下两个字符串:
var str1 = "hello";
var str2 = "ehlol";
Run Code Online (Sandbox Code Playgroud)
如何检查两个字符串是否包含相同的字符?
gur*_*372 38
可能不是最优,但你可以做到
str1.split("").sort().join() == str2.split("").sort().join(); //outputs true
Run Code Online (Sandbox Code Playgroud)
另一个建议的方法在一个注释中(对于字符串长度非常大的优化)
str1.length===str2.length && str1.split("").sort().join() == str2.split("").sort().join(); //first check the length to quickly rule out in case of obvious non-matches
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1007 次 |
| 最近记录: |