foxxy foxxy - 4 months ago 35
Java Question

splitting strings after detected vowels

I'm trying to split words into syllables, but the kind of syllables in my langauge (Setswana). The syllables appear each and every time after a vowel e.g. in "motho" (which means person) syllables are "mo" and "tho". They are separated by vowel, so identifying a vowel is a major key

I need a code that identifies a vowel and splits a part of a word, 1 VOWEL PER SYLLABLE...this is what i have so far:

public void syllable(String word)
int i=0;
int z=0;
for (i=i;i<word.length()-1; i++){
char w = word.charAt(i);
String w1=Character.toString(w);
if(w=='a'|| w=='e' || w=='i' || w=='o' || w=='u')
String[] x= word.split(w1,2);



It is a complicated regular expression concept, but what you want to do is use a positive look-behind as a split:

String[] syllables = word.split("(?<=[aeiou])");

Which, for me, produces:

public static void main(String[] args) {
    for (String word : new String[]{"Motho", "Botswana"}) {
        System.out.println("Word " + word + " Syllables " 
                + Arrays.toString(word.split("(?<=[aeiou])")));

and output:

Word Motho Syllables [Mo, tho]
Word Botswana Syllables [Bo, tswa, na]

Update: About the split, using a positive look-behind: This regex tutorial does a better job than I can do here.

The Java Pattern documentation is light on detail, but you want zero-width positive lookbehind