编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
这题以前在论坛里看到过,,记得当时有一种很短的算法,好像只是用了一个while循环。找了半天没找着,希望大家有知道帮帮忙,谢谢大家了
这题以前在论坛里看到过,,记得当时有一种很短的算法,好像只是用了一个while循环。找了半天没找着,希望大家有知道帮帮忙,谢谢大家了
public class string {
public static void main(String[] args){
byte[] b="我ABC汉DEF".getBytes();//输入一个字符串,将它转换成字节数组
int n=4;//设置你要截取的字节数
String str=new String(b,0,n);//重新创建一个字符串,b是字符创内容,0是截取前指针指向的位置,n是截取字节个数
System.out.println(str);//将截取的字符串打印出来
}
}
这个比你用while的还简单
这个是在你的题目基础上的另外一个类似的题目:http://topic.csdn.net/u/20091210/23/58a37291-fd1a-4fd0-85b7-791f1d75cb7f.html
public String getString(String s,int n) throws UnsupportedEncodingException{
int index = 0; //定义游标位置
StringBuffer ss = new StringBuffer();
for(int i = 0 ; i < n; i++){
if(s.charAt(index)<255 && s.charAt(index)>0 || Character.isDigit(s.charAt(index))){ ss.append(s.charAt(index)); //如果当前字符不输于字母或者是数字,则添加到结果。
index++;
}else{
ss.append(s.charAt(index)); //如果当前字符是汉字,则添加到结果中,游标向前移动一位。
index++;
i++;
}
}
return ss.toString();
}
http://topic.csdn.net/u/20090307/17/564fe3b5-158d-4141-803f-67cd420fb3be.html
int cutLength = 0;
int byteNum = bytes;
byte bt[] = splitStr.getBytes();
System.out.println("Length of this String ===>" + bt.length);
if (bytes > 1) {
for (int i = 0; i < byteNum; i++) {
if (bt[i] < 0) {
cutLength++; }
} if (cutLength % 2 == 0) {
cutLength /= 2;
}else
{
cutLength=0;
}
}
int result=cutLength+--byteNum;
if(result>bytes)
{
result=bytes;
}
if (bytes == 1) {
if (bt[0] < 0) {
result+=2; }else
{
result+=1;
}
}
String substrx = new String(bt, 0, result);
System.out.println(substrx); } public static void main(String args[]) {
String str = "我abc的DEFe呀fgsdfg大撒旦";
int num =3;
System.out.println("num:" + num);
test sptstr = new test();
sptstr.splitIt(str, num);
} }
运行情况:
num:3
Length of this String ===>25
我a num:2
Length of this String ===>25
我 num:1
Length of this String ===>25
我 num:4
Length of this String ===>25
我ab
class SplitString
{
String SplitStr;
int SplitByte;
public SplitString(String str,int bytes)
{
SplitStr=str;
SplitByte=bytes;
System.out.println("The String is:´"+SplitStr+"´;SplitBytes="+SplitByte);
}
public void SplitIt()
{
int loopCount;
loopCount=(SplitStr.length()%SplitByte==0)?(SplitStr.length()/SplitByte):(SplitStr.length()/Split Byte+1);
System.out.println("Will Split into "+loopCount);
for (int i=1;i<=loopCount ;i++ )
{
if (i==loopCount){
System.out.println(SplitStr.substring((i-1)*SplitByte,SplitStr.length()));
} else {
System.out.println(SplitStr.substring((i-1)*SplitByte,(i*SplitByte)));
}
}
}
public static void main(String[] args)
{
SplitString ss = new SplitString("test中dd文dsaf中男大3443n中国43中国人 0ewldfls=103",4);
ss.SplitIt();
}
}
好不容易找到了呵
String _str = "" ;
for (int i=0;i<str.length();i++) {
//先判断每一个字符是不是汉字
if (str.charAt(i).getBytes().length == 2) {
count += 2 ;
} else {
count ++ ;
}
if (count > n) {
break ;
}
_str += str.subString(i,i+1) ;
}
return _str ;
}
String str = "我ABC汉DEF";
byte leng = 6;
char[] len = str.toCharArray();
byte[] by = str.getBytes();
StringReader sr = new StringReader(str);
int lon = sr.read(len, 0, leng-1);
System.out.println(new String(by,0,lon));
public static String getSubStr(String s,int n){
byte[] bb = s.getBytes();
byte[] result=new byte[n];
if(bb[n-1]<0){
bb[n-1]='\0';
}
for(int i=0;i<n;i++){
result[i]=bb[i];
}
return new String(result);
}
package com.yhw.test;import java.util.ArrayList;
import java.util.List;class SplitString {
/*
* 首先按字节数bytes截取字符串splitStr
* 然后判断截取的字符串temp的总字节数是否符合条件,若是则直接加到list中,
* 否则循环地去掉temp最后一个字符,直到符合条件为止
*/
public List<String> SplitStr(String splitStr, int bytes) {
List<String> list = new ArrayList<String>();
int beginIndex = 0;
int endIndex = 0;
while (true) {
String temp;
endIndex = beginIndex + bytes;
if (endIndex >= splitStr.length()) {
endIndex = splitStr.length();
temp = splitStr.substring(beginIndex, splitStr.length());
while (true) {
if (temp.getBytes().length > bytes) {
temp = temp.substring(0, temp.length() - 1);
} else {
beginIndex += temp.length();
list.add(temp);
break;
}
}
temp = splitStr.substring(beginIndex, splitStr.length());
list.add(temp);
break;
}
temp = splitStr.substring(beginIndex, endIndex);
while (true) {
if (temp.getBytes().length > bytes) {
temp = temp.substring(0, temp.length() - 1);
} else {
beginIndex += temp.length();
list.add(temp);
break;
}
}
}
return list;
} public static void main(String[] args) {
SplitString ss = new SplitString();
String splitStr = "YouAre中国China人民哈哈哈";
int bytes = 4;
List<String> list = new ArrayList<String>();
list = ss.SplitStr(splitStr, bytes);
for (String s : list) {
System.out.println(s);
}
}
}