Split Method - Split into 3 parts

Asked

Viewed 304 times

1

I have an instruction to separate two words from a string and put into an array. This instruction is working very well. Below:

public static List<String> converterCamelCase(String original) {

        String[] arraySplit = original.split("(?<=[a-z])(?=[A-Z])"); 
}

Example: "name", result will be "name" (0) and Compound (1).

Only now I need to adjust this instruction to separate into 3 parts - Example: Nomecpfcontribuinte, result "Name"(0) CPF(1) Contributor(2)

How can I do that?

I want to make that same instruction separate two or three words. At the moment I can only separate into UP to 2 parts. Example of words: name - "name", nameCompute - "name", "Compound", numeroCPF - "number", "CPF".

In the case of a string with numbers, it will happen of the method to receive this string formatting, example: recover10First - "recovers", "10", "First".

Is it possible to do this in the same instruction?

  • Important you define well what your needs are - separate the three letters "cpf" there in the middle is not difficult - the problem is if you change your String for other letters, or use numbers, or change their amount. What is your intention?

  • Edith and include more information in your question, instead of answering here in the comments... :)

  • 1

    If possible, give more examples of use (with Strings ) - take advantage to do the tour, is worth a medal!

  • with this CPF everything in capital letters complicates a little, I replaced it and stayed like this https://ideone.com/mPgYh0

  • @Leocaracciolo the problem there is that if appear other acronyms(RG, IR, PIS, etc), would have to treat all in hand, there is too much work.

  • is true so I did not put as an answer :)

  • Thank you Daniel and Leo. I will keep trying to do with regular expression.

  • I think I understand what you want - you’d like the program to recognize the words to separate them. You only get that by using a dictionary. Using [tag:regex], it needs to rely on some well-determined rule to make separations, for example by comma, space, hyphen, etc. Another way is to just leave one uppercase per word ex.: NomeCpfContribuinte it wouldn’t be hard to separate.

  • Hi Daniel, as I am a beginner in java and do not know how to use regular expressions with ease, I did just this, I left only one uppercase per word (Nomecpfcontribuinte). But I want to study more about regular expressions. Thank you so much for coming back.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.