[英]Get all substrings of a string in JavaScript
I have the following function to get all of the substrings from a string in JavaScript.我有以下 function 从 JavaScript 中的字符串中获取所有子字符串。 I know it's not correct but I feel like I am going about it the right way.
我知道这是不正确的,但我觉得我正在以正确的方式去做。 Any advice would be great.
任何建议都会很棒。
var theString = 'somerandomword',
allSubstrings = [];
getAllSubstrings(theString);
function getAllSubstrings(str) {
var start = 1;
for ( var i = 0; i < str.length; i++ ) {
allSubstrings.push( str.substring(start,i) );
}
}
console.log(allSubstrings)
Edit: Apologies if my question is unclear.编辑:如果我的问题不清楚,请道歉。 By substring I mean all combinations of letters from the string (do not have to be actual words) So if the string was 'abc' you could have [a, ab, abc, b, ba, bac etc...] Thank you for all the responses.
substring 我的意思是字符串中所有字母的组合(不必是实际单词)所以如果字符串是'abc',你可以有[a,ab,abc,b,ba,bac等......]谢谢对于所有的回应。
You need two nested loop for the sub strings.子字符串需要两个嵌套循环。
function getAllSubstrings(str) { var i, j, result = []; for (i = 0; i < str.length; i++) { for (j = i + 1; j < str.length + 1; j++) { result.push(str.slice(i, j)); } } return result; } var theString = 'somerandomword'; console.log(getAllSubstrings(theString));
.as-console-wrapper { max-height: 100% !important; top: 0; }
A modified version of Accepted Answer.已接受答案的修改版本。 In order to give the minimum string length for permutation
为了给出置换的最小字符串长度
function getAllSubstrings(str, size) { var i, j, result = []; size = (size || 0); for (i = 0; i < str.length; i++) { for (j = str.length; j - i >= size; j--) { result.push(str.slice(i, j)); } } return result; } var theString = 'somerandomword'; console.log(getAllSubstrings(theString, 6));
Below is a recursive solution to the problem以下是该问题的递归解决方案
let result = []; function subsetsOfString(str, curr = '', index = 0) { if (index == str.length) { result.push(curr); return result; } subsetsOfString(str, curr, index + 1); subsetsOfString(str, curr + str[index], index + 1); } subsetsOfString("somerandomword"); console.log(result);
An answer with the use of substring function.使用子字符串函数的答案。
function getAllSubstrings(str) { var res = []; for (let i = 0; i < str.length; i++) { for (let j = i + 1; j <= str.length; j++) { res.push(str.substring(i, j)); } } return res; } var word = "randomword"; console.log(getAllSubstrings(word));
Below is a simple approach to find all substrings以下是查找所有子字符串的简单方法
var arr = "abcde";
for(let i=0; i < arr.length; i++){
for(let j=i; j < arr.length; j++){
let bag ="";
for(let k=i; k<j; k++){
bag+ = arr[k]
}
console.log(bag)
}
}
function generateALlSubstrings(N,str){
for(let i=0; i<N; i++){
for(let j=i+1; j<=N; j++){
console.log(str.substring(i, j));
}
}
}
function getSubstrings(s){
//if string passed is null or undefined or empty string
if(!s) return [];
let substrings = [];
for(let length = 1 ; length <= s.length; length++){
for(let i = 0 ; (i + length) <= s.length ; i++){
substrings.push(s.substr(i, length));
}
}
return substrings;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.