Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式,用于计数&;是否删除行首的空格?_Java_Regex - Fatal编程技术网

Java正则表达式,用于计数&;是否删除行首的空格?

Java正则表达式,用于计数&;是否删除行首的空格?,java,regex,Java,Regex,搜索了其他问题,未找到任何结果 我已经写了一个正则表达式来删除行首的空格,但是我需要计算它们并将它们放在行首 scan.nextLine(); 上面是正则表达式(它在while循环中)。我在while循环中阅读文本,以检查是否有更多文本,它工作正常,但我不知道如何在删除空格数的情况下打印整数。为什么不这样做 String line = scan.nextLine(); String trimmedLine = line.replaceAll("^\\s+", ""); int spacesRe

搜索了其他问题,未找到任何结果

我已经写了一个正则表达式来删除行首的空格,但是我需要计算它们并将它们放在行首

scan.nextLine();

上面是正则表达式(它在while循环中)。我在while循环中阅读文本,以检查是否有更多文本,它工作正常,但我不知道如何在删除空格数的情况下打印整数。

为什么不这样做

String line = scan.nextLine();
String trimmedLine = line.replaceAll("^\\s+", "");
int spacesRemoved = line.length() - trimmedLine.length();
你喜欢这样吗

 public static void main(String args[]){

            String scan =" Hello World";

              String scan1= scan.replaceAll("^\\s*","");

                   int count = scan.length()-scan1.length();

            System.out.println("Number of Spaces removed at the begining"+count);

         }

试试这个:这将给出前导空格和尾随空格的计数

String str = "  Hello   ";
int strCount = str.length();
要获取前导空格,请执行以下操作:

String ltrim = str.replaceAll("^\\s+","");
System.out.println(":"+ltrim+": spaces at the beginning:" + (strCount-ltrim.length()));
String rtrim = str.replaceAll("\\s+$","");
System.out.println(":"+rtrim+": spaces at the end:" + (strCount-rtrim.length()));
要获取尾随空格,请执行以下操作:

String ltrim = str.replaceAll("^\\s+","");
System.out.println(":"+ltrim+": spaces at the beginning:" + (strCount-ltrim.length()));
String rtrim = str.replaceAll("\\s+$","");
System.out.println(":"+rtrim+": spaces at the end:" + (strCount-rtrim.length()));

如果要计算字符串开头的空格数,请执行以下操作:

String s = "  123456";
int count = s.indexOf(s.trim());

您可以使用Pattern&Matcher,这样您就可以得到匹配的字符串及其长度

String pattern = "\\s+";
String str = "     Hello   ";
Matcher matcher = Pattern.compile(pattern).matcher(str);
if(matcher.find()){
    System.out.println(matcher.group().length());
    str = matcher.replaceAll("");
    System.out.println(str);
} 

subString()和/或indexOf()和/或split()将对您有所帮助。。有什么特别的原因迫使你使用正则表达式吗?它会在行首给出一个整数吗?是的。。。它将根据第一个非空白字符进行分割。您的第一个数组(split()result)将包含所有前导的WhitePSACE。。在上面做arr.length…不,我不需要正则表达式,我只需要删除开头的空格并计数。这会计算模式出现的次数,并且模式匹配多个空格。因此,它不可能计算行首的空格数,除非只有一个空格,如您的示例所示-1@EJP谢谢你的建议,我现在修复了它。有点太简单了:如果字符串只有空格,它将返回0。因此,如果s.trim().isEmpty()返回s.length()。