Home > front end >  How do I split and join string Java
How do I split and join string Java

Time:01-14

I'm using a RegEx to match the string fields below so that I get this order:

  • Data&HoraUTC
  • V
  • ProblemaEspecífico
  • ID
  • DescricaoInformacaoAdicional

I do the first division by removing the blanks, slashes and parentheses. After that, the fields have 1 space between them.

however, I can't separate the fields* V and ProblemaEspecifico* and ID and Descricao/InformacaoAdditional.

Can anybody help me. I believe a little adjustment and the code will work. thank you for your attention and help.

This is the original line.

====================================================================================================================                                                                                                                            
Data & Hora (UTC)   V Problema Específico                    ID (Descricao/InformacaoAdicional)                                                                                                                                                             
====================================================================================================================

The code :

List<String> stringList = Pattern.compile("[^A-Za-z\\&]{2,}")
            .splitAsStream("Data & Hora UTC, V Problema Específico, ID Descricao/InformacaoAdicional")
            .map(String::trim)
            .collect(Collectors.toList());

stringList.forEach(s -> System.out.println(s));

Result:

Data & Hora UTC
V Problema Específico
ID Descricao/InformacaoAdicional

CodePudding user response:

In Java, you can split a string into an array of substrings using the split() method of the String class. The split() method takes a regular expression as an argument, and returns an array of substrings that were separated by the regular expression.

For example, if you want to split a string on every instance of the letter "a", you can use the following code:

String originalString = "Hello, this is a string";
String[] substrings = originalString.split("a");

The resulting array substrings will contain the following substrings: {"Hello, this is ", " string"}.

You can also use the split() method with no arguments to split a string on every instance of whitespace:

String originalString = "Hello, this is a string";
String[] substrings = originalString.split(" ");

The resulting array substrings will contain the following substrings: {"Hello,","this","is","a","string"}.

You can also limit the number of parts after split by adding an additional parameter in split(regex, limit)

For joining a string array you can use join method of String class which takes two parameters a separator and an Iterable:

    String[] words = {"Hello", "world"};
String joinedString = String.join(" ", words); 

The joined string will be "Hello world" with a space separator.

You can also use the join() method on a Stream of strings, which can be useful when working with large collections of strings:

List<String> wordsList = Arrays.asList("Hello", "world");

String joinedString = wordsList.stream().collect(Collectors.joining(" "));

Please note that the join method requires at least Java 8

CodePudding user response:

I don't think you can get a single pattern to do the whole job, so simplest would be to insert the commas yourself for the split to operate on.

So preprocess your string like :

List<String> stringList = Pattern.compile("[^A-Za-z\\&]{2,}")
        .splitAsStream("Data & Hora UTC, V Problema Específico, ID Descricao/InformacaoAdicional"
                .replaceAll(", *([^ ] ))", ", \1,") )
        .map(String::trim)
        .collect(Collectors.toList());

That replaceAll changes your String ready for the split, so :

"Data & Hora UTC, V, Problema Específico, ID, Descricao/InformacaoAdicional"
  • Related