Programing

Java에서 문자열을 동일한 길이의 하위 문자열로 분할

crosscheck 2020. 7. 26. 12:26
반응형

Java에서 문자열을 동일한 길이의 하위 문자열로 분할


"Thequickbrownfoxjumps"Java에서 문자열을 동일한 크기의 하위 문자열로 분할하는 방법 예 : "Thequickbrownfoxjumps"크기가 4와 같으면 출력해야합니다.

["Theq","uick","brow","nfox","jump","s"]

비슷한 질문 :

스칼라에서 문자열을 같은 길이의 하위 문자열로 분할


다음은 정규식 한 줄짜리 버전입니다.

System.out.println(Arrays.toString(
    "Thequickbrownfoxjumps".split("(?<=\\G.{4})")
));

\G이전 일치가 종료 된 위치와 일치하는 너비가 0 인 어설 션입니다. 이 경우 없었다 에는 이전의 일치, 그것은 입력의 시작, 같은 일치 \A. 둘러싼 lookbehind는 마지막 일치의 끝에서 네 문자 인 위치와 일치합니다.

Lookbehind와 \G고급 정규식 기능은 모든 맛에서 지원되는 것은 아닙니다. 또한 \G이를 지원하는 여러 가지 특성에 일관되게 구현되지 않습니다. 이 트릭은 Java , Perl, .NET 및 JGSoft에서 작동하지만 PHP (PCRE), Ruby 1.9+ 또는 TextMate (Oniguruma 모두)에서는 작동 하지 않습니다 . JavaScript /y(sticky flag)는만큼 유연 \G하지 않으며 JS가 lookbehind를 지원하더라도 이러한 방식으로 사용할 수 없습니다.

다른 옵션이있는 경우 반드시이 솔루션을 권장 하지는 않습니다 . 다른 답변의 비정규 솔루션은 더 길 수 있지만 자체 문서화도 가능합니다. 이것은 그것 반대 입니다. ;)

또한 Android에서 작동하지 않으므로 \Glookbehinds 의 사용을 지원하지 않습니다 .


간단한 산술 및 문자열 연산 으로이 작업을 수행하는 것이 매우 쉽습니다.

public static List<String> splitEqually(String text, int size) {
    // Give the list the right capacity to start with. You could use an array
    // instead if you wanted.
    List<String> ret = new ArrayList<String>((text.length() + size - 1) / size);

    for (int start = 0; start < text.length(); start += size) {
        ret.add(text.substring(start, Math.min(text.length(), start + size)));
    }
    return ret;
}

나는 이것을 위해 정규 표현식을 사용할 가치가 있다고 생각하지 않습니다.

편집 : 정규식을 사용하지 않는 이유 :

  • 이것은 정규식의 실제 패턴 일치를 사용하지 않습니다. 방금 계산 중입니다.
  • 대부분의 경우 중요하지 않지만 위의 내용이 더 효율적 이라고 생각 합니다.
  • 다른 장소에서 가변 크기를 사용해야하는 경우 매개 변수 -ick를 기반으로 정규 표현식 자체를 작성하는 반복 또는 도우미 함수가 있습니다.
  • 다른 답변으로 제공된 정규 표현식은 먼저 컴파일되지 않았으며 (잘못된 이스케이프 처리) 작동하지 않았습니다. 내 코드는 처음으로 작동했습니다. 그것은 정규 코드와 일반 코드 IMO의 유용성에 대한 증거입니다.

Google Guava를 사용하면 매우 쉽습니다 .

for(final String token :
    Splitter
        .fixedLength(4)
        .split("Thequickbrownfoxjumps")){
    System.out.println(token);
}

산출:

Theq
uick
brow
nfox
jump
s

또는 배열로 결과가 필요한 경우 다음 코드를 사용할 수 있습니다.

String[] tokens =
    Iterables.toArray(
        Splitter
            .fixedLength(4)
            .split("Thequickbrownfoxjumps"),
        String.class
    );

참고:

참고 : 스플리터 구성은 위에 인라인으로 표시되어 있지만 스플리터는 변경할 수없고 재사용 할 수 있으므로 상수로 저장하는 것이 좋습니다.

private static final Splitter FOUR_LETTERS = Splitter.fixedLength(4);

// more code

for(final String token : FOUR_LETTERS.split("Thequickbrownfoxjumps")){
    System.out.println(token);
}

구글의 구아바 범용 라이브러리를 사용하고 있다면 (그리고 솔직히 새로운 자바 프로젝트 라면 아마 ) Splitter 클래스를 사용하는 것은 그리 쉬운 일이 아닙니다 :

for (String substring : Splitter.fixedLength(4).split(inputString)) {
    doSomethingWith(substring);
}

그게 다야 . 쉽게!


public static String[] split(String src, int len) {
    String[] result = new String[(int)Math.ceil((double)src.length()/(double)len)];
    for (int i=0; i<result.length; i++)
        result[i] = src.substring(i*len, Math.min(src.length(), (i+1)*len));
    return result;
}

public String[] splitInParts(String s, int partLength)
{
    int len = s.length();

    // Number of parts
    int nparts = (len + partLength - 1) / partLength;
    String parts[] = new String[nparts];

    // Break into parts
    int offset= 0;
    int i = 0;
    while (i < nparts)
    {
        parts[i] = s.substring(offset, Math.min(offset + partLength, len));
        offset += partLength;
        i++;
    }

    return parts;
}

(예외 처리) 또는 Apache lang commons ( 예외 처리) substring에서 사용할 수 있습니다.String.class

static String   substring(String str, int start, int end) 

루프 안에 넣으면 갈 수 있습니다.


다음은 Java8 스트림을 사용하는 하나의 라이너 구현입니다.

String input = "Thequickbrownfoxjumps";
final AtomicInteger atomicInteger = new AtomicInteger(0);
Collection<String> result = input.chars()
                                    .mapToObj(c -> String.valueOf((char)c) )
                                    .collect(Collectors.groupingBy(c -> atomicInteger.getAndIncrement() / 4
                                                                ,Collectors.joining()))
                                    .values();

다음과 같은 출력을 제공합니다.

[Theq, uick, brow, nfox, jump, s]

오히려이 간단한 해결책을 원합니다.

String content = "Thequickbrownfoxjumps";
while(content.length() > 4) {
    System.out.println(content.substring(0, 4));
    content = content.substring(4);
}
System.out.println(content);

문자열을 똑같이 뒤로 (예 : 오른쪽에서 왼쪽으로) 분할하려는 경우 (예 :) 1010001111는 다음 [10, 1000, 1111]과 같습니다.

/**
 * @param s         the string to be split
 * @param subLen    length of the equal-length substrings.
 * @param backwards true if the splitting is from right to left, false otherwise
 * @return an array of equal-length substrings
 * @throws ArithmeticException: / by zero when subLen == 0
 */
public static String[] split(String s, int subLen, boolean backwards) {
    assert s != null;
    int groups = s.length() % subLen == 0 ? s.length() / subLen : s.length() / subLen + 1;
    String[] strs = new String[groups];
    if (backwards) {
        for (int i = 0; i < groups; i++) {
            int beginIndex = s.length() - subLen * (i + 1);
            int endIndex = beginIndex + subLen;
            if (beginIndex < 0)
                beginIndex = 0;
            strs[groups - i - 1] = s.substring(beginIndex, endIndex);
        }
    } else {
        for (int i = 0; i < groups; i++) {
            int beginIndex = subLen * i;
            int endIndex = beginIndex + subLen;
            if (endIndex > s.length())
                endIndex = s.length();
            strs[i] = s.substring(beginIndex, endIndex);
        }
    }
    return strs;
}

다음 Java 8 솔루션을 사용합니다.

public static List<String> splitString(final String string, final int chunkSize) {
  final int numberOfChunks = (string.length() + chunkSize - 1) / chunkSize;
  return IntStream.range(0, numberOfChunks)
                  .mapToObj(index -> string.substring(index * chunkSize, Math.min((index + 1) * chunkSize, string.length())))
                  .collect(toList());
}

Java 8 IntStream 을 사용하여 슬라이스 시작 색인을 결정 하는 한 줄짜리 버전이 있습니다 .

String x = "Thequickbrownfoxjumps";

String[] result = IntStream
                    .iterate(0, i -> i + 4)
                    .limit((int) Math.ceil(x.length() / 4.0))
                    .mapToObj(i ->
                        x.substring(i, Math.min(i + 4, x.length())
                    )
                    .toArray(String[]::new);

(같은 자바 (8) 솔루션 있지만, 조금 더 간단) :

public static List<String> partition(String string, int partSize) {
  List<String> parts = IntStream.range(0, string.length() / partSize)
    .mapToObj(i -> string.substring(i * partSize, (i + 1) * partSize))
    .collect(toList());
  if ((string.length() % partSize) != 0)
    parts.add(string.substring(string.length() / partSize * partSize));
  return parts;
}

줄 바꿈이있는 문자열을 처리하는 방법에 대해 @Alan Moore에게 허용 된 솔루션 에 대한 의견을 물었습니다 . 그는 DOTALL 사용을 제안했습니다.

그의 제안을 사용하여 그 작동 방식에 대한 작은 샘플을 만들었습니다.

public void regexDotAllExample() throws UnsupportedEncodingException {
    final String input = "The\nquick\nbrown\r\nfox\rjumps";
    final String regex = "(?<=\\G.{4})";

    Pattern splitByLengthPattern;
    String[] split;

    splitByLengthPattern = Pattern.compile(regex);
    split = splitByLengthPattern.split(input);
    System.out.println("---- Without DOTALL ----");
    for (int i = 0; i < split.length; i++) {
        byte[] s = split[i].getBytes("utf-8");
        System.out.println("[Idx: "+i+", length: "+s.length+"] - " + s);
    }
    /* Output is a single entry longer than the desired split size:
    ---- Without DOTALL ----
    [Idx: 0, length: 26] - [B@17cdc4a5
     */


    //DOTALL suggested in Alan Moores comment on SO: https://stackoverflow.com/a/3761521/1237974
    splitByLengthPattern = Pattern.compile(regex, Pattern.DOTALL);
    split = splitByLengthPattern.split(input);
    System.out.println("---- With DOTALL ----");
    for (int i = 0; i < split.length; i++) {
        byte[] s = split[i].getBytes("utf-8");
        System.out.println("[Idx: "+i+", length: "+s.length+"] - " + s);
    }
    /* Output is as desired 7 entries with each entry having a max length of 4:
    ---- With DOTALL ----
    [Idx: 0, length: 4] - [B@77b22abc
    [Idx: 1, length: 4] - [B@5213da08
    [Idx: 2, length: 4] - [B@154f6d51
    [Idx: 3, length: 4] - [B@1191ebc5
    [Idx: 4, length: 4] - [B@30ddb86
    [Idx: 5, length: 4] - [B@2c73bfb
    [Idx: 6, length: 2] - [B@6632dd29
     */

}

그러나 https://stackoverflow.com/a/3760193/1237974의 @Jon Skeets 솔루션도 좋아 합니다. 모든 사람이 정규 표현식을 똑같이 경험하지 않는 대규모 프로젝트의 유지 관리를 위해 Jons 솔루션을 사용했을 것입니다.


또 다른 무차별 대입 솔루션은

    String input = "thequickbrownfoxjumps";
    int n = input.length()/4;
    String[] num = new String[n];

    for(int i = 0, x=0, y=4; i<n; i++){
    num[i]  = input.substring(x,y);
    x += 4;
    y += 4;
    System.out.println(num[i]);
    }

코드가 부분 문자열로 문자열을 단계별로 이동하는 경우


    import static java.lang.System.exit;
   import java.util.Scanner;
   import Java.util.Arrays.*;


 public class string123 {

public static void main(String[] args) {


  Scanner sc=new Scanner(System.in);
    System.out.println("Enter String");
    String r=sc.nextLine();
    String[] s=new String[10];
    int len=r.length();
       System.out.println("Enter length Of Sub-string");
    int l=sc.nextInt();
    int last;
    int f=0;
    for(int i=0;;i++){
        last=(f+l);
            if((last)>=len) last=len;
        s[i]=r.substring(f,last);
     // System.out.println(s[i]);

      if (last==len)break;
       f=(f+l);
    } 
    System.out.print(Arrays.tostring(s));
    }}

결과

 Enter String
 Thequickbrownfoxjumps
 Enter length Of Sub-string
 4

 ["Theq","uick","brow","nfox","jump","s"]

@Test
public void regexSplit() {
    String source = "Thequickbrownfoxjumps";
    // define matcher, any char, min length 1, max length 4
    Matcher matcher = Pattern.compile(".{1,4}").matcher(source);
    List<String> result = new ArrayList<>();
    while (matcher.find()) {
        result.add(source.substring(matcher.start(), matcher.end()));
    }
    String[] expected = {"Theq", "uick", "brow", "nfox", "jump", "s"};
    assertArrayEquals(result.toArray(), expected);
}

다음은 RegEx 및 Java 8 스트림을 기반으로 한 버전입니다. 그것은 언급 할 가치 Matcher.results()방법은 자바 9부터 사용할 수 있습니다.

테스트가 포함되었습니다.

public static List<String> splitString(String input, int splitSize) {
    Matcher matcher = Pattern.compile("(?:(.{" + splitSize + "}))+?").matcher(input);
    return matcher.results().map(MatchResult::group).collect(Collectors.toList());
}

@Test
public void shouldSplitStringToEqualLengthParts() {
    String anyValidString = "Split me equally!";
    String[] expectedTokens2 = {"Sp", "li", "t ", "me", " e", "qu", "al", "ly"};
    String[] expectedTokens3 = {"Spl", "it ", "me ", "equ", "all"};

    Assert.assertArrayEquals(expectedTokens2, splitString(anyValidString, 2).toArray());
    Assert.assertArrayEquals(expectedTokens3, splitString(anyValidString, 3).toArray());
}

public static String[] split(String input, int length) throws IllegalArgumentException {

    if(length == 0 || input == null)
        return new String[0];

    int lengthD = length * 2;

    int size = input.length();
    if(size == 0)
        return new String[0];

    int rep = (int) Math.ceil(size * 1d / length);

    ByteArrayInputStream stream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_16LE));

    String[] out = new String[rep];
    byte[]  buf = new byte[lengthD];

    int d = 0;
    for (int i = 0; i < rep; i++) {

        try {
            d = stream.read(buf);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(d != lengthD)
        {
            out[i] = new String(buf,0,d, StandardCharsets.UTF_16LE);
            continue;
        }

        out[i] = new String(buf, StandardCharsets.UTF_16LE);
    }
    return out;
}

public static List<String> getSplittedString(String stringtoSplit,
            int length) {

        List<String> returnStringList = new ArrayList<String>(
                (stringtoSplit.length() + length - 1) / length);

        for (int start = 0; start < stringtoSplit.length(); start += length) {
            returnStringList.add(stringtoSplit.substring(start,
                    Math.min(stringtoSplit.length(), start + length)));
        }

        return returnStringList;
    }

참고URL : https://stackoverflow.com/questions/3760152/split-string-to-equal-length-substrings-in-java

반응형