java 字符串转数组(String to Array)
package com.journaldev.util;
import java.util.Arrays;
import java.util.regex.Pattern;
public class StringToArrayExample {
/**
* This class shows how to convert String to String Array in Java
* @param args
*/
public static void main(String[] args) {
String line = "My name is Pankaj";
//using String split function
String[] words = line.split(" ");
System.out.println(Arrays.toString(words));
//using java.util.regex Pattern
Pattern pattern = Pattern.compile(" ");
words = pattern.split(line);
System.out.println(Arrays.toString(words));
}
}
- 数组转List、 判断String数组中是否包含某个字符串
/**
* Java 判断String数组中是否包含某个字符串
*
* @param args
*/
public static void main(String[] args) {
String str = "124,567,345,389,10,33,";
String[] split = str.split(",");
List<String> list = Arrays.asList(split);
System.out.println(list.contains("33") ? "包含" : "不包含");
System.out.println(list.contains("39") ? "包含" : "不包含");
}
文章来源地址https://www.toymoban.com/news/detail-517443.html
文章来源:https://www.toymoban.com/news/detail-517443.html
到了这里,关于java 字符串转数组(String to Array)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!