⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 str.java

📁 jsp实现医院门诊系统
💻 JAVA
字号:
package com.hospital.util;


/**
    *    字符串格式化工具
 
 */

public class str {

    /**
	 * 用于输入的表单字符串转化成HTML格式的文本
     */
    public static String Replace(String source, String oldString, String newString) {
        StringBuffer output = new StringBuffer();

        int lengthOfSource = source.length();   // 源字符串长度
        int lengthOfOld = oldString.length();   // 老字符串长度

        int posStart = 0;   // 开始搜索位置
        int pos;            // 搜索到老字符串的位置

        while ((pos = source.indexOf(oldString, posStart)) >= 0) {
            output.append(source.substring(posStart, pos));

            output.append(newString);
            posStart = pos + lengthOfOld;
        }

        if (posStart < lengthOfSource) {
            output.append(source.substring(posStart));
        }

        return output.toString();
    }

   

    /**
     * 将字符串格式化成 HTML 代码输出
     */
    public static String toHtmlInput(String str) {
        if (str == null)    return null;

        String html = new String(str);

        html = Replace(html, "&", "&amp;");
        html = Replace(html, "<", "&lt;");
        html = Replace(html, ">", "&gt;");

        return html;
    }

    /**
     * 将字符串格式化成 HTML 代码输出
     */
    public static String toHtml(String str) {
        if (str == null)    return null;

        String html = new String(str);

        html = toHtmlInput(html);
        html = Replace(html, "\r\n", "\n");
        html = Replace(html, "\n", "<br>\n");
        html = Replace(html, "\t", "    ");
        html = Replace(html, "  ", " &nbsp;");

        return html;
    }

    /**
     * 将普通字符串格式化成数据库认可的字符串格式
     */
    public static String toSql(String str) {
        String sql = new String(str);
        return Replace(sql, "'", "''");
    }
    //测试字符串的转换
    /*
    public static void main(String[] args) {
        String s = "<html>    ddd";
        Format f = new Format();
        System.out.println(f.toHtmlInput(s));
        System.out.println(f.toHtml(s));
    }
    */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -