Splitting a String with the split method in Java

Arrays are often used in computer programs, it can be said that almost all programs contain arrays of different types. structures. The simplest is the  Structure  contiguous  of which all operations except the addition are done sequentially. The  Removal  is done with a  Access  Direct but on  must  Do the  Offset  other elements.
Counting words in a Channel  character character implies searching for the "space" character. Java makes things easier for us and this is where we notice the advantage of object-oriented, there is a function  Predefined  split, divide in English.

The split method takes a regular expression as input. it divides this Channel  according to the given regular expression and it returns an array which contains substrings. If the expression does not match the  Channel  searched for the resulting array has only one element.


If we want to divide the  Channel  Next: I.am.a.programmer separated by  dots, We should write split("\\." ) and not (split("." ) false ). Simply because we said that the split method takes regular expressions as input. If you don't have any knowledge, just look on the internet. I give you the link directly: 


Regular expressions in java with regex

In our example, we don't need big things. We want to count the number of words separated by one" space"  in a  Channel  of characters. It exists  two ways to achieve this:

1- with the function split

The split method takes the input to  string to be processed and output it must return  Word count:

int  numberwords(String  string)

We declare a painting  String[] words;   to store sub-strings. The latter receives the substrings found by split:

words=string.split(" ");

Finally, return the length of the table words:

return  mots.length;

2- Without  The split

This method seems longer but it is not necessary  Since Java already has the predefined Split function. This is a plus for those who  want to Train.

We declare a word counter and a character.


int  n=0;
char  c;

Then we read all the characters of the  Channel  and we test if one of them is a space without counting the blanks at the beginning and end of the sentence. If yes, increment the counter.


for(int  i = 0; i < string.length(); i++){
    c = chain.charAt(i);
      if(i!=0 & & i!=string.length()-1)
        if(c==' ')
    n++;
}

Finally, the counter is equal to 0 if the  Channel  input is empty and equal to 1  if  The expression does not correspond to the  Channel  searched.

If it is  Superior  to 1 means that there is at least one space.

if(n> 1)
  n++;

Example

" I'm a programmer "
We add 1 because we count the spaces in between. the words( between each two words a space).

public class WordNumberString {

//with the predefined function split
static int numberwords(String string){
int n=0;
String[] words;
//split splits the string into a set of words
//if there is a space and puts them in an array
words = string.split(" ");
return words.length;
}

//without the predefined function split
static int nombremots_naive(String string){
int n=0;
char c;
//browse the entire string
for(int i = 0 ; i < string.length() ; i++){
//character by character
c = string.charAt(i);
//Avoid counting spaces
//at the beginning and at the end
//example: "I am a programmer"
//if we remove this test, the number of words
//will be 6 even though we have 4 words.
if(i!=0 & & i!=string.length()-1)
if(c==' ')
n++;
}
//if the string is not empty
//we add 1 because we
//count the spaces between
//the words i.e. if we have 3 spaces
//then we will have 4 words
if(n> 1)
n++;
return n;
}

public static void main(String[] args) {
String string="I'm a programmer";
System.out.println(nombremots_naive(string));
}
}