编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。程序代码:
package untitled1;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() / SplitByte + 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(); }}结果:
The String is:′test中dd文dsaf中男大3443n中国43中国人0ewldfls = 103′;SplitBytes=4
Will Split into 11
test
中dd文
dsaf
中男大3
443n
中国43
中国人0
ewld
fls
= 10
3
但是我觉得上面结果与题目要求不对,请哪位高手给个正确的程序
package untitled1;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() / SplitByte + 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(); }}结果:
The String is:′test中dd文dsaf中男大3443n中国43中国人0ewldfls = 103′;SplitBytes=4
Will Split into 11
test
中dd文
dsaf
中男大3
443n
中国43
中国人0
ewld
fls
= 10
3
但是我觉得上面结果与题目要求不对,请哪位高手给个正确的程序
解决方案 »
- 请帮忙提供一些java项目,可供学习的??谢谢
- 请高手推荐一些关于Swing的教程或者书籍
- JDK1.5API_CN(全)这个到底要怎么看
- 各位进来看看这个错误是怎么回事???????????????????????????
- 如何将自己编写的UI 的JavaBean 集成到JBuilder7中?
- 在看<thinking in java>的时候遇到一个inner class的问题,请各位帮忙解答一下
- 哪位仁兄做过多媒体,给我一点建议,答者有分!
- 再问skyyoung(路人甲)关于文件存储
- Java Applet读取 Oracle 问题
- 谁知道Jprobe哪里有下
- java GUI 窗口关闭,为什么在windows任务管理器进程没有结束,求解
- String问题
如果理解为有符号数的话,就是负数;而英文占1个字节,符合ASC2码。 class SplitString
{
private String str;
private int byteNum; public SplitString() {}
public SplitString(String str, int byteNum)
{
this .str = str;
this .byteNum = byteNum; }
public void splitIt()
{ byte bt[] = str.getBytes();
System.out.println( " Length of this String ===> " + bt.length);
if (byteNum >= 1 )
{
if (bt[byteNum] < 0 )
{
String substrx = new String(bt, 0 , -- byteNum);
System.out.println(substrx);
} else
{
String substrex = new String(bt, 0 ,byteNum);
System.out.println(substrex);
}
} else
{
System.out.println( " 输入错误!!!请输入大于零的整数: " );
}
}
}
public class TestSplitString
{
public static void main(String args[])
{
String str = " 我ABC汉DEF " ;
int num = 6 ;
SplitString sptstr = new SplitString(str,num);
sptstr.splitIt();
}
}
String substrx = new String(bt, 0 , -- byteNum);中的-- byteNum这个是什么意思?
如果我要循环截取那又该如何?
if (bt[byteNum-1] < 0 ) 会更好点