简体   繁体   English

为什么我的String数组长度是3而不是2?

[英]Why is my String array length 3 instead of 2?

I'm trying to understand regex. 我试图了解正则表达式。 I wanted to make a String[] using split to show me how many letters are in a given string expression? 我想使用split创建一个String []来显示给定的字符串表达式中有多少个字母?

import java.util.*; 
import java.io.*;

public class Main {

  public static String simpleSymbols(String str) {

     String result = "";
     String[] alpha = str.split("[\\+\\w\\+]");
     int alphaLength = alpha.length;
     // System.out.print(alphaLength);

     String[] charCount = str.split("[a-z]");
     int charCountLength = charCount.length;
     System.out.println(charCountLength);
   }
}

My input string is "+d+=3=+s+" . 我的输入字符串是"+d+=3=+s+" I split the string to count the number of letters in string. 我拆分字符串以计算字符串中的字母数。 The array length should be two but I'm getting three. 数组长度应该是两个,但我要三个。 Also, I'm trying to make a regex to check the pattern +b+, with b being any letter in the alphabet? 另外,我正在尝试制作一个正则表达式来检查模式+ b +,其中b是字母表中的任何字母? Is that correct? 那是对的吗?

So, a few things pop out to me: 因此,有几件事突然出现在我眼前:

First, your regex looks correct. 首先,您的正则表达式看起来正确。 If you're ever worried about how your regex will perform, you can use https://regexr.com/ to check it out. 如果您担心正则表达式的性能,可以使用https://regexr.com/进行检查。 Just put your regex on the top and enter your string in the bottom to see if it is matching correctly 只需将您的正则表达式放在顶部,然后在底部输入您的字符串以查看其是否正确匹配

Second, upon close inspection, I see you're using the split function. 其次,仔细检查后,我发现您正在使用split功能。 While it is convenient for quickly splitting strings, you need to be careful as to what you are splitting on. 虽然方便快速分割字符串,但您要注意分割的内容。 In this case, you're removing all of the strings that you were initially looking at, which would make it impossible to find. 在这种情况下,您将删除最初查看的所有字符串,这将导致找不到。 If you print it out, you would notice that the following shows (for an input string of +d+=3=+s+): 如果将其打印出来,则会注意到以下内容(对于+ d + = 3 = + s +的输入字符串):

+
+=3=+
+

Which shows that you accidentally cut out what you were looking to find in the first place. 这表明您不小心切掉了要查找的内容。 Now, there are several ways of fixing this, depending on what your criteria is. 现在,有几种方法可以解决此问题,具体取决于您的标准。

Now, if what you wanted was just to separate on all +s and it doesn't matter that you find only what is directly bounded by +s, then split works awesome. 现在,如果您想要的只是在所有+ s上分开,而只找到直接由+ s限制的东西,则没关系,那么split的效果真棒。 Just do str.split("+") , and this will return you a list of the following (for +d+=3=+s+): 只需执行str.split("+") ,这将为您返回以下列表(对于+ d + = 3 = + s +):

d
=3=
s

However, you can see that this poses a few problems. 但是,您可以看到这带来了一些问题。 First, it doesn't strip out the =3= that we don't want, and second, it does not truly give us values that are surrounded by a +_+ format, where the underscore represents the string/char you're looking for. 首先,它不会去除我们不想要的= 3 =;其次,它并没有真正为我们提供被+ _ +格式包围的值,其中下划线表示您要输入的字符串/字符寻找。

Seeing as you're using +w, you intend to find words that are surrounded by +s. 看到使用+ w时,您打算查找被+ s包围的单词。 However, if you're just looking to find one character, I would suggest using another like [az] or [a-zA-Z] to be more specific. 但是,如果您只是想查找一个字符,我建议您使用另一个字符,例如[az]或[a-zA-Z]更具体。 However, if you want to find multiple alphabetical characters, your pattern is fine. 但是,如果要查找多个字母字符,则可以使用模式。 You can also add a * (0 or more) or a + (1 or more) at the end of the pattern to dictate what exactly you're looking for. 您还可以在模式的末尾添加*(0或更大)或+(1或更大)以指示您要查找的内容。

I won't give you the answer outright, but I'll give you a clue as to what to move towards. 我不会直接给您答案,但是会给您一个提示,告诉您如何发展。 Try using a pattern and a matcher to find the regex that you listed above and then if you find a match, make sure to store it somewhere :) 尝试使用模式和匹配器找到上面列出的正则表达式,然后如果找到匹配项,请确保将其存储在某个地方:)

Also, for future reference, you should always start a function name with a lower case, at least in Java. 另外,为了将来参考,至少在Java中,应始终以小写字母开头的函数名。 Only constants and class names should start in a capital :) 只有常量和类名才能以大写字母开头:)

I am trying to use split to count the number of letters in that string. 我正在尝试使用split来计算该字符串中的字母数。 The array length should be two, but I'm getting three. 数组长度应该是两个,但是我要三个。

The regex in the split functions is used as delimiters and will not be shown in results. 拆分函数中的正则表达式用作定界符,并且不会在结果中显示。 In your case "str.split([az])" means using alphabets as delimiters to separate your input string, which makes three substrings "(+)|d|(+=3=+)|s|(+)". 在您的情况下,“ str.split([az])”表示使用字母作为分隔符来分隔您的输入字符串,这使三个子字符串成为“(+)| d |(+ = 3 = +)| s |(+)”。

If you really want to count the number of letters using "split", use 'str.split("[^az]")'. 如果您真的想使用“ split”来计算字母数,请使用'str.split(“ [^ az]”)'。 But I would recommend using "java.util.regex.Matcher.find()" in order to find out all letters. 但是我建议使用“ java.util.regex.Matcher.find()”以找出所有字母。

Also, I'm trying to make a regex to check the pattern +b+, with b being any letter in the alphabet? 另外,我正在尝试制作一个正则表达式来检查模式+ b +,其中b是字母表中的任何字母? Is that correct? 那是对的吗?

Similarly, check the functions in "java.util.regex.Matcher". 同样,检查“ java.util.regex.Matcher”中的功能。

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

相关问题 如果String是不可变的,那么为什么你必须调用一个方法来获取长度而不是仅仅访问一个变量,比如array.length? - If a String is immutable, then why do you have to call a method to get the length instead of just accessing a variable, like array.length? 为什么我的字节数组显示错误的长度? - Why is my byte array displaying the wrong length? 为什么我的Java String的长度比生成它的byte []数组的长度短? - Why is my java String shorter in length than the byte[] array it was generated from? 为什么我的程序打印“ null”而不是字符串? - Why is my program printing “null” instead of the string? 为什么我的方法是打印 null 而不是数组? - Why is my method printing null instead of the array? 为什么我的程序只显示我的数组之一<string>我的所有 hashmap 数据中的食物而不是实际值?</string> - Why does my program only show one of my Array <String> Food in all of my hashmap data instead of the actual values? 为什么我的 XPath 字符串长度不起作用? (爪哇) - Why's my XPath String-length not working? (Java) 为什么我的合并排序不适用于数组长度10的原因 - is there a reason why my merge sort does not work for array length 10 为什么.length方法不能与Array一起使用? - Why won't my .length method work with Array? 为什么字符串长度为-1 - why string length -1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM