爪哇 - 分裂之后的字符串,什么是数组中的第一个元素? [英] java - after splitting a string, what is the first element in the array?

查看:139
本文介绍了爪哇 - 分裂之后的字符串,什么是数组中的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将字符串分割成单个字母的数组。下面是我做什么,

I was trying to split a string into an array of single letters. Here's what I did,

String str = "abcddadfad"; 
System.out.println(str.length());    //  output: 10  
String[] strArr = str.split("");  
System.out.println(strArr.length);   //  output: 11   
System.out.println(strArr[0]);       // output is nothing 

新阵列并包含所有的字母,但它没有任何索引0,甚至没有一个空格,但仍增加我的数组的大小。任何人都可以解释为什么发生这种情况?

The new array did contain all the letters, however it has nothing at index 0,not even a white space, but still incremented the size of my array. Can anyone explain why this is happening?

推荐答案

考虑拆分前pression 1,2,3,4.split();

Consider the split expression ",1,2,3,4".split(",");

什么?右,一个空字符串的开始。你的情况,你在第一个A,以及为一体的背后前面的无。

What would you expect? Right, an empty-string to start with. In your case you have a 'nothing' in front of the first 'a' as well as one behind it.

更新:评论表明对此的解释是不够的解释(它可能不是)......可是,真的是这样简单:发动机开始在字符串的开头,它会查看是否有什么在它前面的模式相匹配。如果是这样,它分配什么是它背后的分裂的新项目。

Update: comments indicate this explanation is not enough of an explanation (which it may not be)... but, it really is this simple: the engine starts at the beginning of the string, and it looks to see if what's in front of it matches the pattern. If it does, it assigns what's behind it to a new item in the split.

在第一个字符,它有(后面没有),并查看是否有在它前面的(模式)。还有就是,所以它创造了一个比赛。

On the first character, it has "" (nothing behind it), and it looks to see if there's "" (the pattern) in front of it. There is, so it creates a "" match.

有然后移动,它有一个它的后面,并再次,它再次拥有,在它的前面。所以第二个结果是一个字符串。

It then moves on, and it has 'a' behind it, and again, it again has "" in front of it. So the second result is an "a" string.

这是有趣的现象是,如果你使用拆分(,-1)您也将在结果数组的最后一个位置得到一个空字符串结果

An interesting observation is that, if you use split("", -1) you will also get an empty-string result in the last position of the result array.

编辑2:如果我再WRACK我的脑子,并且认为这是一个学术活动(我不会在现实生活中推荐这...)我能想到的只有一个好办法做一个正则表达式拆分()串转化成每个字符串中的的String [] 阵列与1个字符(如反对为char [] - 这等人总是要给予极大的答案....)。

Edit 2: If I wrack my brains further, and consider this to be an academic exercise (I would not recommend this in real life...) I can think of only one good way to do a regex split() of a String into a String[] array with 1 character in each string (as opposed to char[] - which other people have given great answers for....).

String[] chars = str.split("(?<=.)", str.length());

这看起来每个人物的背后,非捕获组,并在那一刹那,然后限制数组大小的字符(你可以离开 str.length()的数量,但是如果你把 1 您将获得在最后一个额外的空间)

This will look behind each character, in a non-capturing group, and split on that, and then limit the array size to the number of characters (you can leave the str.length() out, but if you put -1 you will get an extra space at the end)

借款nitro2k01的(在评论如下)替代它引用字符串的开始和结束时,你可以在可靠分裂:

Borrowing nitro2k01's alternative (below in the comments) which references the string beginning and end, you can split reliably on:

String[] chars = str.split("(?!(^|$))");

这篇关于爪哇 - 分裂之后的字符串,什么是数组中的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆