How to Split a String into Consecutive Substrings of Length at Most 3 in All Possible Ways

How to split a string into consecutive substrings of length at most 3 in all possible ways?

Here's a working function. May be not optimal as I didn't spend much time on it.

str = "1234567890"

def f(s, n)
return [[]] if s.empty?

(1..[n, s.length].min).map{|c| f(s[c..-1], n).map{|a| [s[0, c]] + a}}.inject(&:+)
end

puts f(str, 3).collect{|l| l * "\t"}

EDIT: Made it a bit shorter and the length is now passed as second parameter to function for flexibility.

Split string into strings by length?

>>> x = "qwertyui"
>>> chunks, chunk_size = len(x), len(x)//4
>>> [ x[i:i+chunk_size] for i in range(0, chunks, chunk_size) ]
['qw', 'er', 'ty', 'ui']

possible ways of splitting a string into n substrings

s = 'abcd'

s[1..-1].each_char.reduce([s[0]]) do |arr, c|
space_c = " #{c}"
arr.flat_map { |str| [str + c, str + space_c] }
end
# => ["abcd", "abc d", "ab cd", "ab c d", "a bcd", "a bc d", "a b cd", "a b c d"]

Here's another way (stuffing spaces into the string).

arr = (1..s.size-1).to_a
#=> [1, 2, 3]
s.size.times.flat_map do |n|
arr.combination(n).map do |locs|
scopy = s.dup
locs.reverse_each { |idx| scopy.insert(idx, ' ') }
scopy
end
end
#=> ["abcd", "a bcd", "ab cd", "abc d", "a b cd", "a bc d", "ab c d", "a b c d"]

How can I split a string into segments of n characters?

var str = 'abcdefghijkl';console.log(str.match(/.{1,3}/g));

How split a string into all possible consecutive combinations about a particular delimiter

You can try this:


import java.util.ArrayList;
import java.util.List;

public class CombinationSplit {

public static void main(String[] args) {

List<String> split = split("a\\b\\c\\d", "\\\\");
for (String s : split) {
System.out.println(s);
}
}

private static List<String> split(String s, String delimiter) {
String[] split = s.split(delimiter);
List<String> output = new ArrayList<>();
for (int i = 1; i < split.length; i++) {
int[][] combinations = consecutiveCombinations(split.length, i);
for (int[] combination : combinations) {
output.add(glue(split, combination, delimiter));
}
}
return output;
}

private static String glue(String[] string, int[] indices, String delimiter) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0, indicesLength = indices.length; i < indicesLength - 1; i++) {
int index = indices[i];
stringBuilder.append(string[index]);
stringBuilder.append(delimiter);
}
stringBuilder.append(string[indices[indices.length - 1]]);
return stringBuilder.toString();
}

private static int[][] consecutiveCombinations(int n, int k) {
int count = n - k + 1;
int[][] output = new int[count][k];
for (int i = 0; i < count; i++) {
int[] path = new int[k];
for (int j = 0; j < k; j++) {
path[j] = i + j;
}
output[i] = path;
}
return output;
}

}

Outputs:

a
b
c
d
a\\b
b\\c
c\\d
a\\b\\c
b\\c\\d

Splitting a string at every n-th character

You could do it like this:

String s = "1234567890";
System.out.println(java.util.Arrays.toString(s.split("(?<=\\G...)")));

which produces:

[123, 456, 789, 0]

The regex (?<=\G...) matches an empty string that has the last match (\G) followed by three characters (...) before it ((?<= ))



Related Topics



Leave a reply



Submit