📜  在Java中将字符串拆分为多个子字符串

📅  最后修改于: 2022-05-13 01:54:22.508000             🧑  作者: Mango

在Java中将字符串拆分为多个子字符串

给定一个字符串,它的任务是将字符串拆分为多个子字符串。

Java中的字符串可以是 0 个或多个字符。

例子 :

(a) "" is a String in java with 0 character
(b) "d" is a String in java with 1 character
(c) "This is a sentence." is a string with 19 characters.

子字符串:作为另一个字符串一部分的字符串称为另一个字符串的子字符串。

示例 1:让我们考虑字符串bat 。可能的子串是:

(1) "b"
(2) "ba"
(3) "bat", every string is a substring of itself
(4) "a"
(5) "at"
(6) "t"
(7) ""  , "" is a substring of every string

示例 2:让我们考虑字符串The Cat 。可能的子串是:

(1) "T"
(2) "Th"
(3) "The"
(4) "The "
(5) "The C"
(6) "The Ca"
(7) "The Cat", every string is a substring of itself
(8) "h"
(9) "he"
(10) "he "
(11) "he C"
(12) "he Ca"
(13) "he Cat"
(14) "e"
(15) "e "
(16) "e C"
(17) "e Ca"
(18) "e Cat"
(19) " "
(20) " C"
(21) " Ca"
(22) " Cat"
(23) "C"
(24) "Ca"
(25) "Cat"
(26) "a"
(27) "at"
(28) "t"
(29) ""  , "" is a substring of every string

注:总计 长度为N的字符串的子字符串数是(N * (N + 1)) / 2 。这是在排除空字符串“”之后,因为它是所有字符串的子字符串。

方法:

Let us consider a string which has n characters:
1. For each character at index i (0 to n-1):
        find substrings of length 1, 2, ..., n-i
        

Example: Let our string be cat, here n = 3 (the length of string)
here, 
    index of 'c' is 0
    index of 'a' is 1
    index of 't' is 2
    
Loop from i = 0 to 2: (since n-1 = 2)

When i = 0:
    Substring of length 1 : "c"   
    Substring of length 2 : "ca" 
    Substring of length 3 : "cat" , (substring of length n-i or 3-0 = 3)
    
When i = 1:
    Substring of length 1 : "a"
    Substring of length 2 : "at" , (substring of length n-i or 3-1 = 2)
    
When i = 2:
    Substring of length 1 : "t" , (substring of length n-i or 3-2 = 1)

例子:

Java
// Java Program to split a string into all possible
// substrings excluding the string with 0 characters i.e. ""
  
import java.io.*;
import java.util.ArrayList;
  
class SubstringsOfAString {
  
    // function to split a string into all its substrings
    // and return the list as an object of ArrrayList
    public static ArrayList
    splitSubstrings(String s)
    {
  
        // variables to traverse through the
        // string
        int i, j;
  
        // to store the length of the
        // string
        int stringLength = s.length();
  
        // List object to store the list of
        // all substrings of the string s
        ArrayList subStringList
            = new ArrayList();
  
        // first for loop
        for (i = 0; i < stringLength; i++) {
  
            for (j = i + 1; j <= stringLength; j++) {
  
                subStringList.add(s.substring(i, j));
            }
  
        } // end of first for loop
  
        // returning the list (object
        // of ArrayList) of substrings
        // of string s
        return subStringList;
    }
  
    public static void main(String[] args)
    {
  
        // here "The Cat" is our input string
        String stringInput = new String("The Cat");
  
        ArrayList subStringList
            = SubstringsOfAString.splitSubstrings(
                stringInput);
  
        System.out.println(
            "\nSubstring list printed as an ArrayList : ");
        System.out.println(subStringList);
  
        System.out.println(
            "\n\nAll substrings printed 1 per line : ");
        int count = 1;
  
        // each substring would be printed
        // within double quotes
        for (String it : subStringList) {
            System.out.println("(" + count + ") \"" + it
                               + "\"");
            count++;
        }
    }
}


输出
Substring list printed as an ArrayList : 
[T, Th, The, The , The C, The Ca, The Cat, h, he, he , he C, he Ca, he Cat, e, e , e C, e Ca, e Cat,  ,  C,  Ca,  Cat, C, Ca, Cat, a, at, t]


All substrings printed 1 per line : 
(1) "T"
(2) "Th"
(3) "The"
(4) "The "
(5) "The C"
(6) "The Ca"
(7) "The Cat"
(8) "h"
(9) "he"
(10) "he "
(11) "he C"
(12) "he Ca"
(13) "he Cat"
(14) "e"
(15) "e "
(16) "e C"
(17) "e Ca"
(18) "e Cat"
(19) " "
(20) " C"
(21) " Ca"
(22) " Cat"
(23) "C"
(24) "Ca"
(25) "Cat"
(26) "a"
(27) "at"
(28) "t"

时间复杂度:O(n 2 ) 其中n是字符串的长度

输出 1:当字符串输入 = bat

Substring list printed as an ArrayList : 
[b, ba, bat, a, at, t]


All substrings printed 1 per line : 
(1) "b"
(2) "ba"
(3) "bat"
(4) "a"
(5) "at"
(6) "t"

输出 2:当字符串输入 = The Cat

Substring list printed as an ArrayList : 
[T, Th, The, The , The C, The Ca, The Cat, h, he, he , he C, he Ca, he Cat, e, e , e C, e Ca, e Cat,  ,  C,  Ca,  Cat, C, Ca, Cat, a, at, t]


All substrings printed 1 per line : 
(1) "T"
(2) "Th"
(3) "The"
(4) "The "
(5) "The C"
(6) "The Ca"
(7) "The Cat"
(8) "h"
(9) "he"
(10) "he "
(11) "he C"
(12) "he Ca"
(13) "he Cat"
(14) "e"
(15) "e "
(16) "e C"
(17) "e Ca"
(18) "e Cat"
(19) " "
(20) " C"
(21) " Ca"
(22) " Cat"
(23) "C"
(24) "Ca"
(25) "Cat"
(26) "a"
(27) "at"
(28) "t"