求助中英文字符串截取

spsonic 2007-04-22 12:26:26
搜索了很多都是实现substring(int beginIndex)功能的,不知道substring(int beginIndex,int endIndex)要怎么实现?
...全文
637 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
saiche05 2007-04-25
  • 打赏
  • 举报
回复
ing
lvkai0452 2007-04-25
  • 打赏
  • 举报
回复
学习
yeyi279 2007-04-24
  • 打赏
  • 举报
回复
。。。以下代码,忘了哪里来的转贴了。第一个参数,传入的是要截的中英文字符串,第二个参数,要截取的长度。
如:subString("2我人a",4);

public String subString(String str, int subBytes) {
int bytes = 0; // 用来存储字符串的总字节数
for (int i = 0; i < str.length(); i++) {
if (bytes == subBytes) {
return str.substring(0, i);}
char c = str.charAt(i);
if (c < 256) {
bytes += 1; // 英文字符的字节数看作1
}else {
bytes += 2; // 中文字符的字节数看作2
if(bytes - subBytes == 1){
return str.substring(0, i);
}
}
}
return str;
}
hudingchen 2007-04-23
  • 打赏
  • 举报
回复
思路:字符串中包含有汉字、字母、数字和其它特殊字符。其中汉字存放2字节,且每个字节的ASCII值均小于0,其它字符的ASCII均大于等于0。当前定位为index。先判断此位置字符是否为字母,如果是则很好处理;关键问题在于如果是汉字,则从此汉字向前后查找,直到遇上第一个字母,对此字母到汉字的距离对2求余数,若为0,则index是汉字的后一个字节,若不为0则index为汉字的前一个字符。
代码如下:

public class Test {
public static void main(String[] args){
String str = "A中b国日日";
System.out.println(subString(str,5,10));
}
static String subString(String str,int beginIndex,int endIndex){
byte[] bt = str.getBytes();
int len = bt.length;
int tempBegin = beginIndex - 1;
int tempEnd = endIndex - 1;
if (bt[tempBegin] >= 0 && bt[tempEnd] >= 0){
return new String(bt,tempBegin,endIndex - beginIndex + 1);
}
else if(bt[tempBegin] >= 0 && bt[tempEnd] < 0){
tempEnd = findPosition(str,tempEnd);
int end = Math.abs(endIndex - 1 - tempEnd) % 2;
if (end != 0){
return new String(bt,tempBegin,endIndex - beginIndex);
}else{
return new String(bt,tempBegin,endIndex - beginIndex + 1);
}
}
else if(bt[tempBegin] < 0 && bt[tempEnd] >= 0){
tempBegin = findPosition(str,tempBegin);
int begin = Math.abs(tempBegin - beginIndex + 1) % 2;
if (begin != 0){
return new String(bt,beginIndex,endIndex - beginIndex);
}else{
return new String(bt,beginIndex - 1,endIndex - beginIndex + 1);
}
}
else if(bt[tempBegin] < 0 && bt[tempEnd] < 0){
tempBegin = findPosition(str,tempBegin);
if (tempBegin == -1){
return null;//字符串全部为汉字,我没做处理,很简单。
}
tempEnd = findPosition(str,tempEnd);
int begin = Math.abs(tempBegin - beginIndex + 1) % 2;
int end = Math.abs(endIndex - 1 - tempEnd) % 2;
if (begin != 0 && end != 0){
return new String(bt,beginIndex - 1,endIndex - beginIndex + 1 - 1);
}else if(begin != 0 && end == 0){
return new String(bt,beginIndex - 1,endIndex - beginIndex + 1);
}else if(begin == 0 && end != 0){
return new String(bt,beginIndex,endIndex - beginIndex - 1);
}else if(begin == 0 && end == 0){
return new String(bt,beginIndex,endIndex - beginIndex);
}
}
else{
return null;
}
return null;
}
static int findPosition(String str,int index){//查找从index开始,遇到第一个字母的位置。
byte[] bt = str.getBytes();
int toBegin = index;
int toEnd = index;
while(true){
if (toBegin == -1) break;
if(bt[toBegin] >= 0) return toBegin;
else toBegin--;
}
while(true){
if (toEnd == bt.length) break;
if(bt[toEnd] == 0) return toEnd;
else toEnd++;
}
return -1;//字符串中全部为汉字
}
}
日文2000系统下 测试过,基本没有问题。
Sunny319 2007-04-22
  • 打赏
  • 举报
回复
java处理字符串时候是变成一个字符数组,然后汉字和英文都是一个字符而已
lanseliuying 2007-04-22
  • 打赏
  • 举报
回复
那如果是“我市”两个字,你要取substring(0,1);怎么办?
把一个汉字算两个字符,你得取出汉字的一半,那取出的是什么?乱玛
spsonic 2007-04-22
  • 打赏
  • 举报
回复
下班了自己顶下
spsonic 2007-04-22
  • 打赏
  • 举报
回复
但是我要的不是这个结果,汉字算两个字符,英文算一个字符,
比如
String str="ab我是c积极";
String sub=str.substring(1,6);
System.out.println(sub);
结果要的是:
b我是
而不是:
b我是c积

现在处理定长数据时候碰到了问题,有什么解决办法?
notepads 2007-04-22
  • 打赏
  • 举报
回复
刚刚测试是可以的..比如楼上的运行结果就是:
b我是c
lanseliuying 2007-04-22
  • 打赏
  • 举报
回复
怎么不行?绝对可以
import java.util.*;
class Test
{
public static void main(String args[])
{
String str="ab我是c积极";
String sub=str.substring(1,5);
System.out.println(sub);
}
}
spsonic 2007-04-22
  • 打赏
  • 举报
回复
谢谢LS,可能我没说清楚,我要的是处理中英文混合字符串截取,直接用substring处理不行。。。
jerrycccc2323 2007-04-22
  • 打赏
  • 举报
回复
substring 方法

返回位于 String 对象中指定位置的子字符串。

strVariable.substring(start, end)
"String Literal".substring(start, end)
参数

start

指明子字符串的起始位置,该索引从 0 开始起算。

end

指明子字符串的结束位置,该索引从 0 开始起算。
说明

substring 方法将返回一个包含从 start 到最后(不包含 end )的子字符串的字符串。

substring 方法使用 start 和 end 两者中的较小值作为子字符串的起始点。例如, strvar.substring(0, 3) 和 strvar.substring(3, 0) 将返回相同的子字符串。

如果 start 或 end 为 NaN 或者负数,那么将其替换为0。

子字符串的长度等于 start 和 end 之差的绝对值。例如,在 strvar.substring(0, 3) 和 strvar.substring(3, 0) 返回的子字符串的的长度是 3。
示例

下面的示例演示了 substring 方法的用法。

function SubstringDemo(){
var ss; // 声明变量。
var s = "The rain in Spain falls mainly in the plain..";
ss = s.substring(12, 17); // 取子字符串。
return(ss); // 返回子字符串。
}

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧