获取java中的split的长度[duplicate]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取java中的split的长度[duplicate]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
有一个字符串,我用空格分割它,我想得到最后一个。
String test = "Hey Hi Hello"; // Defining "test" String
test = test.split(" ")[2]; // Now "test" is "Hello"
System.out.print(test); // prints Hello
我应该这样做以获得“test”字符串的最后一个字。但我知道字符串的长度。如果我不知道字符串的长度怎么办?我应该在[ ]
之间写下什么来得到最后一个字?
例如,当我从网页获取数据时,我现在不知道价值是什么。
答案
test.split
返回一系列String
s。只需将其保存在某处而不是立即使用它并检查其length
。
String test = "Hey Hi Hello";
String[] words = test.split(" ");
test = words[words.length - 1];
System.out.print(test);
另一答案
String[] temp = test.split(" "); //this will split whole string into array using white space.
test = temp[temp.length-1]; //gets the element at the last index of temp array
另一答案
另一种方法是避免分裂并直接切断最后一个单词。
String text = "One Two Three";
text = text.substring(text.lastIndexOf(" ") + 1);
这样,你从最后一个空格中取出字符串直到String的结尾
以上是关于获取java中的split的长度[duplicate]的主要内容,如果未能解决你的问题,请参考以下文章
Caused by org hibernate DuplicateMappingException Duplicat
Pandas处理dataframe的文本数据列:使用str属性获取数据列的字符串方法类split函数基于指定分隔符拆分数据列的内容为列表使用len计算每个列表的长度
java split(String regex, int limit) 的使用