[英]Java : Split string on same character
我想拆分一個字符串,使用“”但是.split()
給了我數組中的空字符串,這是我不想要的。
我試過這個:
String[] arr = " hello world! b y e ".split(" ",0);
Output:
[“”、“”、“”、“”、“”、“你好”等等......
預期 Output:
["你好","世界,","b","y","e"]
我怎樣才能做到這一點?
首先,修剪字符串,然后用一個或多個空格 ( \s+
) 分割。
String string = " hello world! b y e ";
String[] arr = string.trim().split("\\s+");
// output: ["hello", "world!", "b", "y", "e"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.