来个简单的, 我觉得lz没有把题目说清楚,或者说全。public class SplitString { public static void main(String args[]){ System.out.println(split("11111", 4)); }
public static String split(String str, int size){ if(size < 0){ System.out.println("size is illegal."); return null; }else if(size >= str.length()){ return str; }else{ return str.substring(0, size); } } }
上一个没考虑传入的字符串是为null的情况: /** * @author bzwm * */ public class SplitString { public static void main(String args[]) { System.out.println(split("11111", 4)); } public static String split(String str, int size) { if (size < 0) { System.out.println("size is illegal."); return null; } else if (str != null && size >= str.length()) { return str; } else if (str != null) { return str.substring(0, size); } else { return null; } } }
小弟来实现下5楼的意思,不知道是不是LZ想问的。小弟自己调试结果正确。如有那些需要改进请指出。 import java.io.BufferedReader; import java.io.*;public class ToSubString { public static void main(String[] args) { System.out.print("请输入要分解的字符串: "); String putInString = putIn(); System.out.print("请输入要分割的字符长度: "); int intI = Integer.parseInt(putIn()); int i = 0; int j = intI; int m = putInString.length(); if( intI < m ) { while( j <= m ){ System.out.print( putInString.substring(i,j) + " "); i = i + intI; j += intI; } if( i<m ) { System.out.println(putInString.subSequence(i, m)); } }else { System.out.println(putInString); } } //键盘输入方法。 private static String putIn() { String str = ""; try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); str = br.readLine(); }catch(IOException e){ } return str; } }
我晕。直接贴过来竟然变成这个格式的。 重新编辑下格式如下://两个包只用一个java.io.*就可以了。刚开始修错添重了。import java.io.*; public class ToSubString { public static void main(String[] args) { System.out.print("请输入要分解的字符串: "); String putInString = putIn(); System.out.print("请输入要分割的字符长度: "); int intI = Integer.parseInt(putIn()); int i = 0; int j = intI; int m = putInString.length(); if( intI < m ) { while( j <= m ){ System.out.print( putInString.substring(i,j) + " "); i = i + intI; j += intI; } if( i <m ) { System.out.println(putInString.subSequence(i, m)); } }else { System.out.println(putInString); } } //键盘输入方法。 private static String putIn() { String str = ""; try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); str = br.readLine(); }catch(IOException e){} return str; } } //忽然发现也可以对中文分组。嘿嘿。第一次在CSDN发程序贴。请多指教。
StringSplit函数实现分组.返回值是一个ArrayList里面装有已分完组的字符串 import java.util.ArrayList; public class temp { public static void main(String[] args) { for (String s : StringSplit("abcdefghijklmnopqrstuvwxyz", 4)) System.out.println(s); } static ArrayList<String> StringSplit(String s, int n) { ArrayList<String> splits = new ArrayList<String>(); assert s != null && n > 0; int pos; for (pos = 0; pos + n < s.length(); pos += n) { splits.add(s.substring(pos, pos + n)); } splits.add(s.substring(pos, s.length())); return splits; } }
/** * add char from right of string/keep char from site-0 of string * * @param s * @param def * @param length * @return */ public String add2StrLeft2Right(String s, String def, int length) { if (s == null) { s = ""; } if (def == null || def.length() < 1) { def = " "; } if (s.length() >= length) { s = s.substring(0,length); } else { StringBuffer bf = new StringBuffer(); bf.append(s); for (int i = 0; i < (length - s.length()); i++) { bf.append(def); } s = bf.toString(); } return s; } /** * add char from left of string/keep char from last of string * * @param s * @param def * @param length * @return */ public String add2StrRight2Left(String s, String def, int length) { if (s == null) { s = ""; } if (def == null || def.length() < 1) { def = " "; } if (s.length() >= length) { s = s.substring(s.length() - length); } else { StringBuffer bf = new StringBuffer(); for (int i = 0; i < (length - s.length()); i++) { bf.append(def); } bf.append(s); s = bf.toString(); } return s; }
这个方法将输入的字符串(包括占两个字节的汉字)按照输入的字节数分割。 例如:str=“ABC我DE”,subBytes=4则应该输出ABC,半个字节的“我”不输出。 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; }
我觉得lz没有把题目说清楚,或者说全。public class SplitString {
public static void main(String args[]){
System.out.println(split("11111", 4));
}
public static String split(String str, int size){
if(size < 0){
System.out.println("size is illegal.");
return null;
}else if(size >= str.length()){
return str;
}else{
return str.substring(0, size);
}
}
}
/**
* @author bzwm
*
*/
public class SplitString {
public static void main(String args[]) {
System.out.println(split("11111", 4));
} public static String split(String str, int size) {
if (size < 0) {
System.out.println("size is illegal.");
return null;
} else if (str != null && size >= str.length()) {
return str;
} else if (str != null) {
return str.substring(0, size);
} else {
return null;
}
}
}
import java.io.BufferedReader;
import java.io.*;public class ToSubString { public static void main(String[] args) {
System.out.print("请输入要分解的字符串: ");
String putInString = putIn();
System.out.print("请输入要分割的字符长度: ");
int intI = Integer.parseInt(putIn());
int i = 0;
int j = intI;
int m = putInString.length();
if( intI < m ) {
while( j <= m ){
System.out.print( putInString.substring(i,j) + " ");
i = i + intI;
j += intI;
}
if( i<m ) {
System.out.println(putInString.subSequence(i, m));
}
}else {
System.out.println(putInString);
}
}
//键盘输入方法。
private static String putIn() {
String str = "";
try{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
}catch(IOException e){
}
return str;
}
}
重新编辑下格式如下://两个包只用一个java.io.*就可以了。刚开始修错添重了。import java.io.*; public class ToSubString { public static void main(String[] args) {
System.out.print("请输入要分解的字符串: ");
String putInString = putIn();
System.out.print("请输入要分割的字符长度: ");
int intI = Integer.parseInt(putIn());
int i = 0;
int j = intI;
int m = putInString.length();
if( intI < m ) {
while( j <= m ){
System.out.print( putInString.substring(i,j) + " ");
i = i + intI;
j += intI;
}
if( i <m ) {
System.out.println(putInString.subSequence(i, m));
}
}else {
System.out.println(putInString);
}
}
//键盘输入方法。
private static String putIn() {
String str = "";
try{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
}catch(IOException e){}
return str;
}
} //忽然发现也可以对中文分组。嘿嘿。第一次在CSDN发程序贴。请多指教。
StringSplit函数实现分组.返回值是一个ArrayList里面装有已分完组的字符串
import java.util.ArrayList;
public class temp {
public static void main(String[] args) {
for (String s : StringSplit("abcdefghijklmnopqrstuvwxyz", 4))
System.out.println(s);
} static ArrayList<String> StringSplit(String s, int n) {
ArrayList<String> splits = new ArrayList<String>();
assert s != null && n > 0;
int pos;
for (pos = 0; pos + n < s.length(); pos += n) {
splits.add(s.substring(pos, pos + n));
}
splits.add(s.substring(pos, s.length()));
return splits;
}
}
* add char from right of string/keep char from site-0 of string
*
* @param s
* @param def
* @param length
* @return
*/
public String add2StrLeft2Right(String s, String def, int length) {
if (s == null) {
s = "";
}
if (def == null || def.length() < 1) {
def = " ";
}
if (s.length() >= length) {
s = s.substring(0,length);
} else {
StringBuffer bf = new StringBuffer();
bf.append(s);
for (int i = 0; i < (length - s.length()); i++) {
bf.append(def);
}
s = bf.toString();
}
return s;
} /**
* add char from left of string/keep char from last of string
*
* @param s
* @param def
* @param length
* @return
*/
public String add2StrRight2Left(String s, String def, int length) {
if (s == null) {
s = "";
}
if (def == null || def.length() < 1) {
def = " ";
}
if (s.length() >= length) {
s = s.substring(s.length() - length);
} else {
StringBuffer bf = new StringBuffer();
for (int i = 0; i < (length - s.length()); i++) {
bf.append(def);
}
bf.append(s);
s = bf.toString();
}
return s;
}
例如:str=“ABC我DE”,subBytes=4则应该输出ABC,半个字节的“我”不输出。
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;
}