Copy the first N words in a string in java -


i want select first n words of text string. have tried split() , substring() no avail. want select first 3 words of following prayer , copy them variable.

for example if have string:

string greeting = "hello example" 

i want variable z first 3 words that

z = "hello is" 

    string mystring = "copying first n numbers of words string";     string [] arr = mystring.split("\\s+");           //splits words & assign arr[]  ex : arr[0] -> copying ,arr[1] -> first           int n=3; // number of words need         string nwords="";          // concatenating number of words required         for(int i=0; i<n ; i++){              nwords = nwords + " " + arr[i] ;                  }      system.out.println(nwords); 



note : here .split() function returns array of strings computed splitting given string around matches of given regular expression

so if write code follows

string mystring = "1234m567m98723651";
string[] arr = mystring.split("m"); //idea : split words if 'm' presents

then answers :
1234 , 567 stored array.

this doing storing split values given array. first split value store arr[0], second goes arr[1].

later part of code concatenating required number of split words

hope can idea this!!!
thank you!


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -