StringUtils sample

The following sample provides String utility methods for working with the Java™ API.

The utility methods are:
replaceString
This function takes three arguments, the source string, the string to be matched match, and the string to be replaced replace. The function returns a new string that results from replacing the first occurrence of match with replace in the source String.
escapeForJS
This function adds escape characters into the string, as necessary, so that the string can be used in JavaScript. For example, a \ character is converted to \\, \\ is converted to \\\\, and \n is converted to \\n.
escapeForCSV
If the given string contains a comma (,), newline character (\n), CRLF character (\r), or double quotation marks, this function returns a string that has double quotation marks at the beginning and end. Also, any embedded double quotation marks have another double quotation mark added to them. The string abdc,asjdfh, "asdfdas" would become "abdc,asjdfh, ""asdfdas""".
escapeForHTML
When isAscii is true, this function searches the string for HTML special characters and replaces them with $#[asciicode];. When isAscii is false, the HTML type control characters in the given string are escaped as follows: < is converted to &#60, > is converted to &#62, \ is converted to &#34, ' is converted to &#39, and \\ is converted to &#92. Continuous space characters are converted to &nbsp.
escapeWithHTMLEntities
This function takes a string and two integers, beg and end. The two integers define a character range. The characters outside of this range are converted to HTML escape sequences. For example, if the range does not include the numeric representation of the letter A (65), then any A in the given string is converted to &#65.
package com.ibm.ccd.api.samplecode;


import java.io.*;
import java.util.*;

/**
 * Class StringUtils
 *
 */
public class StringUtils
    {

    public static String replaceString(String s, String sMatch, String sReplace)
    {
        if (sReplace == null)
            sReplace = "";

        if (sMatch == null || "".equals(sMatch) || sMatch.equals(sReplace))
            return s;

        if (s == null || s.equals(""))
        {
            return "";
        }

        int i = 0;
        int j = s.indexOf(sMatch);

        if (j < 0)
        {
            return s;
        }

        StringBuffer sb = new StringBuffer(s.length());

        while (true)
        {
            sb.append(s.substring(i, j));
            sb.append(sReplace);

            i = j + sMatch.length();
            j = s.indexOf(sMatch, i);

            if (j < 0)
            {
                sb.append(s.substring(i));
                break;
            }
        }

        return sb.toString();
    }


    public static String escapeDelimiter(String s, String sEscaper, String sDelimiter, String sBackendDelimiter)
    {
        if (s == null || "".equals(s))
            return "";

        StringBuffer sbResult = new StringBuffer();
        for (int i = 0; i < s.length(); i++)
        {
            if (s.startsWith("" + sEscaper + sEscaper, i))
            {
                sbResult.append(sEscaper);
                i++;
            }
            else if (s.startsWith("" + sEscaper + sDelimiter, i))
            {
                sbResult.append(sDelimiter);
                i++;
            }
            else if (s.startsWith("" + sDelimiter, i))
            {
                sbResult.append(sBackendDelimiter);
            }
            else
            {
                sbResult.append(s.charAt(i));
            }
        }

        return sbResult.toString();
    }


    public static String escapeForJS(String txt)
    {
        return escapeForJS(txt, false);
    }



    public static String escapeForJS(String txt, boolean useDosCRLF)
    {
        txt = StringUtils.replaceString(txt, "\\", "\\\\");

        if (useDosCRLF)
        {
            txt = StringUtils.replaceString(txt, "\r", "");
            txt = StringUtils.replaceString(txt, "\n", "\\r\\n");
        }
        else
        {
            txt = StringUtils.replaceString(txt, "\r", "");
        }

        txt = StringUtils.replaceString(txt, "\n", "\\n");
        txt = StringUtils.replaceString(txt, "'", "\\'");
        txt = StringUtils.replaceString(txt, "\"", "\\\"");
        return txt;
    }




    public static String escapeForCSV(String sArg)
    {
        StringBuffer sb = new StringBuffer();
        if (sArg != null)
        {
            if ((sArg.indexOf(",") >= 0) || (sArg.indexOf("\n") >= 0) || (sArg.indexOf("\r") >= 0)
                || (sArg.indexOf("\"") >= 0))
            {
                sb.append("\"");
                sb.append(StringUtils.replaceString(sArg, "\"", "\"\""));
                sb.append("\"");
            }
            else
            {
                sb.append(sArg);
            }
        }

        return sb.toString();
    }

    public static String escapeForHTML(String sArg, boolean isAscii)
    {
        String s;
        if(isAscii)
        {
            s = replaceString(sArg, "&", "&");
            s = replaceString(s, "<", "<");
            s = replaceString(s, ">", ">");
            s = replaceString(s, "\"", """);
            s = replaceString(s, "'", "'");
        }
        else
        {
            s = replaceString(sArg,"&","&");
            s = replaceString(s, "<", "<");
            s = replaceString(s, ">", ">");
            s = replaceString(s, "\"", """);
            s = replaceString(s, "'", "'");
        }
        return preserveSpaceRuns(s);
    }

    public static String preserveSpaceRuns(String s)
    {
        if (s.indexOf("  ") < 0)        
// Quick check for no runs of spaces
            return s;
        else
        {
            int imax = s.length();
            StringBuffer sb = new StringBuffer(imax + imax);
            for (int i = 0; i < imax; i++)
            {
                char c = s.charAt(i);
                sb.append(c);
                if (c == ' ')
                {
                    for (int j = i + 1; j < imax && s.charAt(j) == c; j++)
                    {
                        sb.append(" ");
                        i = j;
                    }
                }
            }
            return sb.toString();
        }
    }



    public static String escapeWithHTMLEntities(String s, int beg, int end)
    {
        StringBuffer sbResult = new StringBuffer();

        for (int i = 0; i < s.length(); i++)
        {
            int ch = (int)s.charAt(i);
            if (ch < beg || ch > end)
                sbResult.append("&#" + ch + ";");
            else
                sbResult.append(s.charAt(i));
        }

        return sbResult.toString();
    }

}