Java 如何将字符串转换为每个包含一个字符的字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1372599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:17:24  来源:igfitidea点击:

How to convert a String into an array of Strings containing one character each

javaarraysstring

提问by gingergeek

I have a small problem here.I need to convert a string read from console into each character of string. For example string: "aabbab" I want to this string into array of string.How would I do that?

我这里有一个小问题。我需要将从控制台读取的字符串转换为字符串的每个字符。例如字符串:“aabbab”我想将此字符串转换为字符串数组。我该怎么做?

回答by VonC

If by array of String you mean array of char:

如果字符串数组是指字符数组:

public class Test
{
    public static void main(String[] args)
    {
        String test = "aabbab ";
        char[] t = test.toCharArray();

        for(char c : t)
            System.out.println(c);    

        System.out.println("The end!");    
    }
}  

If not, the String.split()function could transform a String into an array of String

如果不是,该String.split()函数可以将一个字符串转换为一个字符串数组

See those String.splitexamples

看那些String.split例子

/* String to split. */
String str = "one-two-three";
String[] temp;

/* delimiter */
String delimiter = "-";
/* given string will be split by the argument delimiter provided. */
temp = str.split(delimiter);
/* print substrings */
for(int i =0; i < temp.length ; i++)
  System.out.println(temp[i]);

The input.split("(?!^)")proposed by Joachimin his answeris based on:

input.split("(?!^)")提议勒夫他的回答是基于:

  • a '?!' zero-width negative lookahead (see Lookaround)
  • the caret '^' as an Anchorto match the start of the string the regex pattern is applied to
  • ' ?!' 零宽度负前瞻(参见Lookaround
  • 插入符号 ' ^' 作为Anchor以匹配正则表达式模式应用于的字符串的开头

Any character which is not the first will be split. An empty string will not be split but return an empty array.

任何不是第一个字符的字符都将被拆分。空字符串不会被拆分,而是返回一个空数组。

回答by VonC

You can use String.split(String regex):

您可以使用 String.split(String regex):

String input = "aabbab";
String[] parts = input.split("(?!^)");

回答by Joachim Sauer

String[] result = input.split("(?!^)");

What this does is split the input String on all empty Strings that are not preceded by the beginning of the String.

这样做是在所有空字符串上拆分输入字符串,这些字符串的开头不是字符串的开头。

回答by DaveJohnston

You mean you want to do "aabbab".toCharArray(); ? Which will return an array of chars. Or do you actually want the resulting array to contain single character string objects?

你的意思是你想做 "aabbab".toCharArray(); ? 这将返回一个字符数组。或者您真的希望结果数组包含单个字符串对象?

回答by JuanZe

Use toCharArray() method. It splits the string into an array of characters:

使用 toCharArray() 方法。它将字符串拆分为一个字符数组:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#toCharArray%28%29

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#toCharArray%28%29

String str = "aabbab";
char[] chs = str.toCharArray();

回答by cbalawat

String x = "stackoverflow";
String [] y = x.split("");