Java 中可以使用 String 类的 split() 方法来对字符串进行分割,其中传入的参数为分割符号。

例如,对于以下字符串:

'String str = 'apple,banana,grape,orange';'

可以使用逗号作为分割符号进行分割:

'String[] arr = str.split(',');'

这样就将字符串分割成了一个数组,每个元素分别为:

'arr[0] = 'apple';' 'arr[1] = 'banana';' 'arr[2] = 'grape';' 'arr[3] = 'orange';'

除了逗号,还可以使用其他符号作为分割符号,例如空格、冒号、分号等。如果要使用特殊字符作为分割符号,需要进行转义,例如:

'String str = 'hello.world';' 'String[] arr = str.split('.');'

这样就可以使用点作为分割符号进行分割了。

Java 字符串分割:使用 split() 方法分割字符串

原文地址: https://www.cveoy.top/t/topic/mkgu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录