本文共 4623 字,大约阅读时间需要 15 分钟。
方法一
//jdk1.4.2.05
-
-
-
-
-
-
- private static String getTitleToTen(String s, int length) throws Exception
- {
-
- byte[] bytes = s.getBytes("Unicode");
- int n = 0;
- int i = 2;
- for (; i < bytes.length && n < length; i++){
- if (i % 2 == 0){
- n++;
- }else{
- if (bytes[i] != 0){
- n++;
- }
- }
- }
-
-
-
-
-
-
-
- if (i % 2 == 1){
- i = i + 1;
- }
- String eside = ".................................................................";
- byte[] byteEside = eside.getBytes("Unicode");
- String title = "";
- if (bytes[i-1] == 0){
- title = new String(bytes, 0, i, "Unicode")+new String(byteEside,0,40,"Unicode");
- }else{
- title = new String(bytes, 0, i, "Unicode")+new String(byteEside,0,38,"Unicode");
- }
- return title;
- }
//方法改进
-
-
-
-
-
-
- private static String getTitleToTen(String s, int length) throws Exception
- {
-
- String title = "";
- s = s.trim();
- byte[] bytes = s.getBytes("Unicode");
- int n = 0;
- int i = 2;
- int chineseNum = 0;
- int englishNum = 0;
- for (; i < bytes.length && n < length; i++){
- if (i % 2 == 0){
- n++;
- }else{
- if (bytes[i] != 0){
- n++;
- chineseNum++;
- }else{
- englishNum++;
- }
- }
- }
-
-
-
-
-
-
-
- if (i % 2 == 1){
- i = i + 1;
- }
-
- if (bytes[i-1] == 0){
- englishNum++;
-
- }else if (englishNum % 2 != 0){
- chineseNum++;
- }
- String eside = ".................................................................";
- String str = new String(bytes,0,i,"Unicode");
- StringBuffer ssss = new StringBuffer(str);
- ssss.append(eside);
- byte[] byteTitle = ssss.toString().getBytes("Unicode");
- int lll = (length*4-4)-2*chineseNum;
- title = new String(byteTitle,0,lll,"Unicode");
- return title;
- }
//jdk1.6.0.06
-
-
-
-
-
-
- public static String bSubstring(String s, int length) throws Exception
- {
-
- byte[] bytes = s.getBytes("Unicode");
- int n = 0;
- int i = 2;
- for (; i < bytes.length && n < length; i++){
-
- if (i % 2 == 1){
- n++;
- }
- else{
-
- if (bytes[i] != 0){
- n++;
- }
- }
-
- }
-
-
-
-
-
-
-
-
-
-
- if (i % 2 == 1){
- i = i + 1;
- }
- return new String(bytes, 0, i, "Unicode");
- }
方法二
- package proc;
-
- public class Tools {
- public Tools() {
- }
-
-
-
-
-
-
-
-
- public static String splitString(String str, int len) {
- return splitString(str, len, ".......");
- }
-
-
-
-
-
-
-
-
-
-
- public static String splitString(String str,int len,String elide) {
- if (str == null) {
- return "";
- }
- byte[] strByte = str.getBytes();
- int strLen = strByte.length;
-
- if (len >= strLen || len < 1) {
- return str;
- }
-
-
-
- int count = 0;
- for (int i = 0; i < len; i++) {
- int value = (int) strByte[i];
- if (value < 0) {
- count++;
- }
- }
- if (count % 2 != 0) {
- len = (len == 1) ? len + 1 : len - 1;
- }
- return new String(strByte, 0, len) + elide.trim();
- }
-
-
-
- public static void main(String[] args) {
-
- Tools cs = new Tools();
-
-
- String s ="aaas学位英语专区 学s位英语专区 学s位英语专区 学位英语专区";
- try{
- System.out.println(cs.splitString(s, 20));
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
方法三
-
-
-
-
-
-
-
-
-
-
- public static String substring(String str, int toCount,String more)throws Exception{
- int reInt = 0;
- String reStr = "";
- if (str == null) return "";
- char[] tempChar = str.toCharArray();
- for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) {
- String s1 = str.valueOf(tempChar[kk]);
- byte[] b = s1.getBytes();
- reInt += b.length;
- reStr += tempChar[kk];
- }
- if (toCount == reInt || (toCount == reInt - 1))
- reStr += more;
- return reStr;
- }
转载地址:http://mblvi.baihongyu.com/