简体   繁体   English

如何在Java中将String转换为没有空格的char数组?

[英]How to convert String to char array without spaces in Java?

Hi everybody I writing java code which counts characters.大家好,我正在编写计算字符的 java 代码。 When I convert String using toCharArray I cant use regex to eliminate whitespaces.当我使用 toCharArray 转换 String 时,我不能使用正则表达式来消除空格。

    System.out.println( "Enter a string:" ); 
    String input = scanner.nextLine();

    // tokenize the input
    char[] chars = input.toCharArray();

This:这个:

input.replaceAll(" ", "").toCharArray()

Probably better to use length() to count the chars though...不过,使用length()来计算字符可能更好......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM