Dividir una string en un número de substrings en Java

Dada una string, la tarea es dividir la string en varias substrings .

Una string en Java puede tener 0 o más caracteres. 

Ejemplos:

(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.

Substring : una string que forma parte de otra string se denomina substring de la otra string.

Ejemplo 1: Consideremos el murciélago de cuerda . Las posibles substrings son:

(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

Ejemplo 2: Consideremos la string The Cat . Las posibles substrings son: 

(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

Nota: El número total de substrings de una string de longitud N es (N * (N + 1)) / 2 . Esto es después de excluir la string vacía «» porque es una substring de todas las strings.

Acercarse:

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)

Ejemplo: 

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 ArrayList
    public static ArrayList<String>
    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<String> subStringList
            = new ArrayList<String>();
  
        // 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<String> 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++;
        }
    }
}
Producción

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"

Complejidad de tiempo : O(n 2 ) donde n es la longitud de una string  

Salida 1: cuando la string Input = 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"

Salida 2: cuando la string de entrada = el gato

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"

Publicación traducida automáticamente

Artículo escrito por farhanhaider418 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *