Java split方法
Stringstr="A,,,,,,,"String[]list=str.split(",");list.length=1;list={"A"};有不有办法分割成这样的?...
String str = "A,,,,,,,"
String[] list = str.split(",");
list.length = 1 ;
list = {"A"};
有不有办法分割成这样的?
list = {"A", "", "", "", "", ""}; 展开
String[] list = str.split(",");
list.length = 1 ;
list = {"A"};
有不有办法分割成这样的?
list = {"A", "", "", "", "", ""}; 展开
4个回答
展开全部
楼上正解。下面是java文档中关于split(两参数)函数的说明。
public String[] split(String regex, int limit)
Splits this string around matches of the given regular expression.
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
public String[] split(String regex, int limit)
Splits this string around matches of the given regular expression.
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.*;
public class Chartest
{
public static void main(String[] args){
String str = "A,,,,,,,";
String[] list = str.split("");
for(String s:list)
System.out.println(s);
}
}
public class Chartest
{
public static void main(String[] args){
String str = "A,,,,,,,";
String[] list = str.split("");
for(String s:list)
System.out.println(s);
}
}
追问
这个分割的东西都不太对了. 是以空串来分割的了.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String[] list = str.split(",", -1);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询