字符串编码--中文字符前后加<>
来源:互联网 发布:李咏主持功力知乎 编辑:程序博客网 时间:2024/06/05 17:16
"abc中国d美国"编码后是"abc<>中国<>d<>美国<>"
public class Main { private String addSingal(String str) { if (str == null) { return null; } int n = str.length(); if (n == 1) { if (isLetter(str.charAt(0))) return str; return "<>" + str + "<>"; } String res = ""; boolean flag = false;//表示当前字符的前一个字符是否是中文,如果是中文则是true,如果是英文则是false char c; for (int i = 0; i < n; i++) { c = str.charAt(i); if (!isLetter(c)) { if (flag == false) { res = res + "<>" + c; flag = true; } else { res = res + c; flag = true; } } else { if (flag == true) { res = res + "<>" + c; flag = false; } else { res = res + c; flag = false; } } }//for if (!isLetter(str.charAt(n - 1))) return res + "<>";//最后一个字符是中文,末尾需要加<> return res; }//addSingal private boolean isLetter(char c) { if (c - 'a' >= 0 && c - 'z' <= 0) return true; return false; }//isLetter public static void main(String[] args) { System.out.println(new Main().addSingal("abc中国d美国"));//abc<>中国<>d<>美国<> System.out.println(new Main().addSingal("abc中国d美国e"));//abc<>中国<>d<>美国<>e System.out.println(new Main().addSingal("abc中国de"));//abc<>中国<>de System.out.println(new Main().addSingal("abc"));//abc System.out.println(new Main().addSingal("中"));//<>中<> }}
阅读全文
0 0
- 字符串编码--中文字符前后加<>
- 招银中文字符前后加<>
- Python 字符串编码中中文字符注意事项
- QT:QString 字符串和中文字符编码
- Python中文编码问题(字符串前面加'u')
- Python中文编码问题(字符串前面加'u')
- 字符串去除前后字符方式
- 在文本段落的前后加字符
- PHP字符串翻转函数 支持中文字符(GBK编码)
- Python字符串编码+MySQLdb中的中文字符问题
- 中文字符编码
- JAVA中文字符编码
- URL中文字符编码
- 中文字符编码问题
- Java 中文字符编码
- perl去除字符串前后的字符
- ruby字符串同时删除前后两个字符
- 去掉字符串的空格、前后字符
- 用bochs调试mbr-初步调试
- 1121. Damn Single (25)
- 2017年9月10日周记
- 【翻译】ASP.NET Core 文档目录
- 用两个栈实现队列
- 字符串编码--中文字符前后加<>
- 554. Brick Wall。
- Android四大组件之Service
- mysql索引含义及分类
- codevs 1535 封锁阳光大学 二分图 解题报告
- 二叉树中的一些概念
- Work2
- 技术决策的相对性
- if选择结构和while循环及for循环