java格式化字符串,在指定位置插入指定字符串,兼容中英文以及特殊字符,例如:换行,用于解决生成pdf换行问题等问题

原因: 由于html转pdf时,不能自动换行,因此才有下面的代码.

注释:完全模拟html页面的自动换行!

package test;

import java.io.UnsupportedEncodingException;

/**
 * 解决pdf换行问题,在指定位置插入指定字符串,兼容中英文以及特殊字符
 *
 * @author xg君
 *
 */
public class app {

    public static void main(String[] args) throws UnsupportedEncodingException {
        System.out.println(addStr(10, "<br/>", "as阿萨德dsa阿斯蒂芬fladadasdsjf阿斯蒂芬ljdsljkjlfdsklfd啥地方都是skljdsasfasdfads"));
    }

    /**
     * 插入方法
     *
     * @param num
     *            每隔几个字符插入一个字符串
     * @param splitStr
     *            待指定字符串
     * @param str
     *            原字符串
     * @return 插入指定字符串之后的字符串
     * @throws UnsupportedEncodingException
     */
    public static String addStr(int num, String splitStr, String str) throws UnsupportedEncodingException {
        StringBuffer sb = new StringBuffer();
        String temp = str;

        int len = str.length();
        while (len > 0) {
            int idx = getEndIndex(temp, num);
            sb.append(temp.substring(0, idx + 1)).append(splitStr);
            temp = temp.substring(idx + 1);
            len = temp.length();
        }

        return sb.toString();
    }

    /**
     * 两个数字/英文
     *
     * @param str
     *            字符串
     * @param num
     *            每隔几个字符插入一个字符串
     * @return int 最终索引
     * @throws UnsupportedEncodingException
     */
    public static int getEndIndex(String str, double num) throws UnsupportedEncodingException {
        int idx = 0;
        double val = 0.00;
        // 判断是否是英文/中文
        for (int i = 0; i < str.length(); i++) {
            if (String.valueOf(str.charAt(i)).getBytes("UTF-8").length >= 3) {
                // 中文字符或符号
                val += 1.00;
            } else {
                // 英文字符或符号
                val += 0.50;
            }
            if (val >= num) {
                idx = i;
                if (val - num == 0.5) {
                    idx = i - 1;
                }
                break;
            }
        }
        if (idx == 0) {
            idx = str.length() - 1;
        }
        return idx;
    }
}

效果:

时间: 2024-07-30 13:42:41

java格式化字符串,在指定位置插入指定字符串,兼容中英文以及特殊字符,例如:换行,用于解决生成pdf换行问题等问题的相关文章

利用RandomAccessFile类在指定文件指定位置插入内容

package File; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; /*利用RandomAccessFile类在指定文件指定位置插入内容.*/ public class InsertContent { public static void ins

在Word指定位置插入富文本域值(html文本)

遇到此问题,首先想到的就是各种百度.结果度娘了一会并没有发现有用的有效的解决方法,哎,看来还得靠自己啊. 首先整理了下手头上的资源,一是HtmlAgilityPack,专门解析Html文本用的:二是我有ASPOSE.Word. 再整理下思路:在Word中插入Html,首先有一点是肯定的,Word跟Html都是Document结构,这点应该是没啥怀疑的.如此的话就感觉好办多了,无非就是Document上插入几个节点,在Html插入节点的方式完全可以运用到此地方. 那么第一种解决方法就是:首先把Wo

如何在JS数组特定索引处指定位置插入元素?

需求: 将一个元素插入到现有数组的特定索引处.听起来很容易和常见,但需要一点时间来研究它. // 原来的数组var array = ["one", "two", "four"];// splice(position, numberOfItemsToRemove, item)// 拼接函数(索引位置, 要删除元素的数量, 元素)array.splice(2, 0, "three"); // www.jbxue.comarray;

sql中从指定位置截取指定长度字符串

1. 字符串函数应用 --从指定索引截取指定长度的字符串 SELECT substring('abcdefg',2,5) --获取字符串中指定字符的索引(从1开始) select charindex(',','ab,cdefg') --实际应用中的语句 select proId,color,substring(FacePath,0,charindex(',',FacePath)) as FacePath from proselect where id=1000000 2. 日期函数应用 --获取

关于文本输入框获取光标位置以及指定位置插入内容

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> </head> <body> &l

简单顺序表的插入,删除,指定位置,指定元素的插入删除操作

头文件 SeqList.h #ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include<stdio.h> #include<string.h> #include<assert.h> #define MAX_SIZE 10 typedef int DataType; typedef unsigned int size_t; typedef struct SeqList { DataType array[MAX_SIZE]; s

php 在字符串指定位置插入新字符

因为项目用到DataTable表格加载后台数据,要连表查询虚拟机选中的策略状态,所以想到先把策略表内容取出来,组成一个'<select><option value="1"></option>[n个option]</select>'字符串,在遍历虚拟机列表时把他的策略值拼成 'value="1"' 这样的字符串,再利用explode()和implode() 函数,组成新的字符串返回给前台,就实现了选中状态. 1 $opt

java如何实现替换指定位置的指定字符串的功能

/**  * @创建日期 2013-07-15  * @创建时间 14:25:59  * @版本号 V 1.0  */ public class CosTest {     public static void main(String[] args) {         String sql = "select * from teacher  where id = ? and name = ?";         System.out.println(replaceString(sql

在JS数组指定位置插入元素

很多与数组有关的任务听起来很简单,但实际情况并不总是如此,而开发人员在很多时候也用不到他.最近我碰到了这样一个需求: 将一个元素插入到现有数组的特定索引处.听起来很容易和常见,但需要一点时间来研究它. // 原来的数组 var array = ["one", "two", "four"]; // splice(position, numberOfItemsToRemove, item) // 拼接函数(索引位置, 要删除元素的数量, 元素) ar