Will transform string to 'pig Latin', e.g. - Java java.lang

Java examples for java.lang:String Replace

Description

Will transform string to 'pig Latin', e.g.

Demo Code


//package com.java2s;

public class Main {
    /** Will transform string to 'pig Latin', e.g. 'banana' -> 'anana-bay', 
     * or 'car' -> 'ar-cay'.//from   w  w w.j  ava2  s.c  o m
     * @param str Given string which you want to transform.
     * @return Transformed form of given string (first letter of str will be
     * moved behind hyphen (which will be immediately after 'str') and
     * 'ay' will be affixed). If empty string was given, '-ay' will be returned
     * (it's natural). */
    public static String toPigLatin(String str) {
        if (str.length() < 1)
            return "-ay";
        char prefix = str.charAt(0);
        return str.substring(1) + "-" + prefix + "ay";
    }
}

Related Tutorials