"This is my first sentence. thanks for share!"
把上面的句子按截出来,每次截10个字符,而且不可把单词分开截。
把上面的句子按截出来,每次截10个字符,而且不可把单词分开截。
解决方案 »
- 对同一个函数,有2个线程需要调用这个函数,那么这2个线程得到的函数的入口地址,一样吗?为什么?
- 初学者的问题!关于布局管理器的!java就那几个管理器吗?
- 如何设置jComBox中的当前显示的值为指定定啊?
- 请问我在编译时遇到以下问题,怎办?
- 谁遇到过这种错,跪求解决
- 急求,中文问题,为什么写文件都变成了单字节
- 请教大家关于RMI的部署问题
- 关于一个线程同步的问题.....
- 在一个List中放若干个Map,怎么穷举出来所有的Map的value的组合
- 开发基于web的数据库,你常选择什么开发工具,web服务器和数据库系统?!
- 服务器端关闭Socket的疑问
- javaSE:判断一个文本框的内容是否为空?
package newToday;public class splitString { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub String str="This is my first sentence. thanks for share!";
spiltIt(str);
}
public static void spiltIt(String str)
{
int count=0,i;
String reg="\\b";
String[]strs=str.split(reg);
for(i=0;i<strs.length;i++)
{
if(strs[i].equals(" "))
{
System.out.println("空格");
continue;
}
count+=strs[i].length();
if(count>=10)
{
break;
}
}
System.out.println("--"+strs[2]+"--");
StringBuffer sb=new StringBuffer();
for(int j=0;j<=i;j++)
{
sb.append(strs[j]);
//sb.append(" ");
}
System.out.println(sb);
}}
这个符合吗??
第一次取得的结果是 The name
那么再加上后面的条件
---->此题无解!
/**
* @author Administrator
*
*/
public class SplitWord { /**
* @param args
*/
public static void main(String[] args) {
String words ="This is my first sentence. thanks for share! i'm successd please give me money 100 RMB!OK?";
StringBuffer sb = new StringBuffer();
int count = 0 ;
for (int i = 0; i < words.length(); i++) {
if(count < words.length()){
char a = words.charAt(count);
count++;
sb.append(a);
if((count % 10 == 0 && count != 0)||(count == words.length())){
char preCh= words.charAt(count-1);
char nextCh=((count+1 < words.length()))? words.charAt(count+1) : ' ';
if(preCh == ' ' || nextCh == ' ' || a == ' '){
System.out.println(sb);
}else{
for(int j = count;j<= words.length() ;j++){
char currentCh_= words.charAt(j);
char preCh_= words.charAt(j-1);
char nextCh_=((j+1)<words.length())? words.charAt(j+1) :' ';
sb.append(currentCh_);
count++;
if(currentCh_ == ' ' || preCh_ == ' ' || nextCh_ == ' '){
String strsb = sb.toString();
System.out.println(strsb);
break;
}
}
}
sb.delete(0, words.length());
}
}else{
break;
}
}
}}
/*
This is my
first sentence.
thanks
for share!
i'm successd
please
give
me money 100
RMB!OK?
*/楼上的,超过十个字符要回退,输出第二行...
String text = "This is my first sentence. thanks for share!";
int head = 0; //记录首位置
int foot = 9;//记录尾
while(foot <= text.length()) {
//判断尾是否为空格,如果为空格就foot向后退一格
if(text.charAt(foot+1) != ' ') {
foot--;
continue;
}
String str = text.substring(head,foot+1);
System.out.println(str.trim());
head = foot+1;
foot = head+10;
}
//输出最后的一部分
System.out.println(text.substring(head).trim());
int head = 0; //记录首位置
int foot = 9;//记录尾
while(foot <= text.length()) {
//判断尾是否为空格,如果为空格就foot向后退一格
if(text.charAt(foot+1) != ' ') {
foot--;
continue;
}
String str = text.substring(head,foot+1);
System.out.println(str.trim());
head = foot+1;
foot = head+10;
}
//输出最后的一部分
System.out.println(text.substring(head).trim());