如何分割字符串数组? [英] How to split a String array?

查看:100
本文介绍了如何分割字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

意向是采取当前行(字符串,其中包含逗号)替换为(修剪空格)空格,最后分裂存储字符串元素到数组。

为什么不这项工作?

 的String [] = TEXTLINE currentInputLine.replace(\\\\ S,).split();


解决方案

我想你想的replaceAll而不是取代。

的replaceAll(\\\\ S,)将删除所有的空格,而不仅仅是冗余的。如果这不是你想要的,你应该尝试的replaceAll(\\\\ S +,\\\\ S)或类似的东西。

Intention is to take a current line (String that contains commas), replace white space with "" (Trim space) and finally store split String elements into the array.

Why does not this work?

String[] textLine = currentInputLine.replace("\\s", "").split(",");

解决方案

I think you want replaceAll rather than replace.

And replaceAll("\\s","") will remove all spaces, not just the redundant ones. If that's not what you want, you should try replaceAll("\\s+","\\s") or something like that.

这篇关于如何分割字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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