/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utility;

import java.util.Vector;

/**
 *
 * @author Ali
 */

public class StringUtil {
    /* Ref: http://forums.sun.com/thread.jspa?threadID=721672 */
    public static String[] split(String str, String delim) {
        String splitStr[] =  null;

        if (str != null)
        {
            int length = str.length();
            int first = 0;
            Vector lista = new Vector();

            if(str.indexOf(delim) != -1) {
                for(int i = 0; i < length; i++) {
                    if(i + delim.length() <= length) {
                        if(str.substring(i, i + delim.length()).equals(delim)) {
                            lista.addElement(str.substring(first, i));
                            first = i + delim.length();
                        }
                    }
                }
                if(!str.endsWith(delim)) {
                    lista.addElement(str.substring(first, length));
                }
            }
            else {
                lista.addElement(str);
            }

            splitStr = new String[lista.size()];

            for(int i = 0; i < lista.size(); i++) {
                splitStr[i] = lista.elementAt(i).toString();
            }
        }

        return splitStr;
    }

    /* Ref: http://www.glodde.com/blog/default.aspx?id=19&t=Snippet-J2ME-Replace */
    public static String replace(String str, String searchStr, String replacementStr)
    {
        StringBuffer strBuffer = new StringBuffer();
        int pos = 0;

        while ((pos = str.indexOf(searchStr)) != -1)
        {
            strBuffer.append(str.substring(0, pos) + replacementStr);
            str = str.substring(pos + searchStr.length());
        }

        strBuffer.append(str);
        return strBuffer.toString();
    }
}
