在每第 n 个位置拆分一个字符串 [英] Split a string, at every nth position

查看:23
本文介绍了在每第 n 个位置拆分一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个正则表达式在每个第三个位置分割一个字符串:

I use this regex to split a string at every say 3rd position:

String []thisCombo2 = thisCombo.split("(?<=\G...)");

其中 G 后面的 3 个点表示要拆分的每第 n 个位置.在这种情况下,3 个点表示每 3 个位置.一个例子:

where the 3 dots after the G indicates every nth position to split. In this case, the 3 dots indicate every 3 positions. An example:

Input: String st = "123124125134135145234235245"
Output: 123 124 125 134 135 145 234 235 245.

我的问题是,如何让用户控制必须拆分字符串的位置数?换句话说,我如何制作这 3 个点,由用户控制的 n 个点?

My question is, how do i let the user control the number of positions where the string must be split at? In other words, how do I make those 3 dots, n dots controlled by the user?

推荐答案

为了大幅提升性能,另一种方法是在循环中使用 substring():

For a big performance improvement, an alternative would be to use substring() in a loop:

public String[] splitStringEvery(String s, int interval) {
    int arrayLength = (int) Math.ceil(((s.length() / (double)interval)));
    String[] result = new String[arrayLength];

    int j = 0;
    int lastIndex = result.length - 1;
    for (int i = 0; i < lastIndex; i++) {
        result[i] = s.substring(j, j + interval);
        j += interval;
    } //Add the last bit
    result[lastIndex] = s.substring(j);

    return result;
}

示例:

Input:  String st = "1231241251341351452342352456"
Output: 123 124 125 134 135 145 234 235 245 6.

它不像 stevevls 的解决方案那么短,但它效率更高(请参阅下面),我认为以后会更容易调整,当然取决于你的情况.

It's not as short as stevevls' solution, but it's way more efficient (see below) and I think it would be easier to adjust in the future, of course depending on your situation.

2,000 个字符的长字符串 - 间隔为 3.

2,000 characters long string - interval is 3.

split("(?<=\G.{" + count + "})") 性能(以毫秒为单位):

split("(?<=\G.{" + count + "})") performance (in miliseconds):

7, 7, 5, 5, 4, 3, 3, 2, 2, 2

splitStringEvery() (substring()) 性能(以毫秒为单位):

splitStringEvery() (substring()) performance (in miliseconds):

2, 0, 0, 0, 0, 1, 0, 1, 0, 0

<小时>

2,000,000 个字符的长字符串 - 间隔为 3.


2,000,000 characters long string - interval is 3.

split() 性能(以毫秒为单位):

split() performance (in miliseconds):

207, 95, 376, 87, 97, 83, 83, 82, 81, 83

splitStringEvery() 性能(以毫秒为单位):

splitStringEvery() performance (in miliseconds):

44, 20, 13, 24, 13, 26, 12, 38, 12, 13

<小时>

2,000,000 个字符的长字符串 - 间隔为 30.


2,000,000 characters long string - interval is 30.

split() 性能(以毫秒为单位):

split() performance (in miliseconds):

103, 61, 41, 55, 43, 44, 49, 47, 47, 45

splitStringEvery() 性能(以毫秒为单位):

splitStringEvery() performance (in miliseconds):

7, 7, 2, 5, 1, 3, 4, 4, 2, 1

<小时>

结论:

splitStringEvery() 方法快得多(即使在 Java 7u6 中的变化),并且随着间隔变大而升级.

The splitStringEvery() method is a lot faster (even after the changes in Java 7u6), and it escalates when the intervals become higher.

即用型测试代码:

pastebin.com/QMPgLbG9

这篇关于在每第 n 个位置拆分一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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