fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String english = " Good morning";
  13. String arabic = "الْجُمْلَةُ الاسْمِيَّةُ";
  14. String kannada = "ಕನ್ನಡ ಕೀಲಿಮಣೆ";
  15.  
  16. String[] engWords = splitToWords(english);
  17. english += " split: ";
  18. for(String word : engWords) english += "\'"+word+"\', ";
  19.  
  20. String[] arabWords = splitToWords(arabic);
  21. arabic += " split: ";
  22. for(String word : arabWords) arabic += "\'"+word+"\', ";
  23.  
  24. String[] kanaWords = splitToWords(kannada);
  25. kannada += " split: ";
  26. for(String word : kanaWords) kannada += "\'"+word+"\', ";
  27.  
  28. System.out.println(english);
  29. System.out.println(arabic);
  30. System.out.println(kannada);
  31. }
  32.  
  33. private static String[] splitToWords(String src){
  34. String[] tmpWords = new String[src.length()];
  35. int idx = -1;
  36. boolean appendingToWord = false;
  37. for(char c : src.toCharArray()){
  38. boolean isLetter = Character.isLetter(c);
  39.  
  40. if(isLetter){
  41. if(!appendingToWord) {
  42. idx++;
  43. tmpWords[idx] = "";
  44. appendingToWord = true;
  45. }
  46. } else {
  47. idx++;
  48. tmpWords[idx] = "";
  49. appendingToWord = false;
  50. }
  51.  
  52. tmpWords[idx] += c;
  53. }
  54.  
  55. String[] words = new String[idx+1];
  56.  
  57. System.arraycopy(tmpWords, 0, words, 0, idx+1);
  58.  
  59. return words;
  60. }
  61. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
  Good morning split: ' ', ' ', 'Good', ' ', 'morning', 
الْجُمْلَةُ الاسْمِيَّةُ split: 'ال', 'ْ', 'ج', 'ُ', 'م', 'ْ', 'ل', 'َ', 'ة', 'ُ', ' ', 'الاس', 'ْ', 'م', 'ِ', 'ي', 'َ', 'ّ', 'ة', 'ُ', 
ಕನ್ನಡ ಕೀಲಿಮಣೆ split: 'ಕನ', '್', 'ನಡ', ' ', 'ಕ', 'ೀ', 'ಲ', 'ಿ', 'ಮಣ', 'ೆ',