简体   繁体   English

如何递归一个字符串以验证它仅包含第一个字符之后的数字

[英]How to recurse a String to validate that it contains only numbers after the first character

I need to create a recursive method that takes a String parameter. 我需要创建一个采用String参数的递归方法
I need to verify that the first letter in the string is a lowercase character, and then verify that everything that comes after the first letter is a number. 我需要验证字符串中的第一个字母是小写字符,然后验证第一个字母之后的所有内容都是数字。

So it should be like this a1234 . 所以应该像这样a1234

I've tried creating a base case, smaller base case, and a general case, but can't seem to figure out the right way to style it: 我尝试创建一个基本案例,一个较小的基本案例和一个一般案例,但似乎无法找出正确的样式设置方式:

public void digitCheck(String s) {

   if () //To Check that first character is a letter

   else if ()To check that everything after the first letter is a number

   else //Invalid
}

I need the code to report whether it's a valid string if the first character is a lower case letter and everything after that is a number . 如果第一个字符是小写字母 ,之后的所有字符都是数字 ,则我需要代码来报告它是否为有效字符串。

Example: 例:
a123 -> valid . a123 > 有效
ab123 -> invalid . ab123 > 无效

使用String.matches()方法:

boolean valid = s.matches(".\\d+");

For solving this problem with recursion for your pattern you can do: 为了为您的模式递归解决此问题,您可以执行以下操作:

  • start from the end and 从头开始,
  • check the last element 检查最后一个元素
  • remove the last element 删除最后一个元素
  • call all remaining part 呼叫所有剩余部分

It should be checking until one element will be passed to the method -> make final validation if it is a lower letter. 应该检查直到将一个元素传递给方法->如果它是一个小写字母,则进行最终验证。

Also, StringUtils class is used from commons lang library. 此外, StringUtils的类是从使用公地郎库。

Here is code snippet: 这是代码片段:

public class StringValidationDemo {
    public boolean validateStringRecursive(String str) {
        if (str.length() == 1) {
            return StringUtils.isAlpha(str) && StringUtils.isAllLowerCase(str);
        }

        String lastIndex = str.substring(str.length() - 1);
        return StringUtils.isNumeric(lastIndex)
                && validateStringRecursive(str.substring(0, str.length() - 1));
    }

    public static void main(String[] args) {
        List<String> words = Arrays.asList("a123", "ab123", "123ab", "A123", "aaa", "123");

        StringValidationDemo demo = new StringValidationDemo();
        for (String word : words) {
            System.out.printf("validation for: %s = %s\n",
                    word, demo.validateStringRecursive(word));
        }
    }
}

Output: 输出:

validation for: a123 = true
validation for: ab123 = false
validation for: 123ab = false
validation for: A123 = false
validation for: aaa = false
validation for: 123 = false

I think you could ommit the first character in the string, and then just check with Integer.parseInt(String). 我认为您可以省略字符串中的第一个字符,然后仅检查Integer.parseInt(String)。 so it would look something like: 所以看起来像这样:

public static boolean isNumeric(String strNum) {
    try {
        double d = Integer.parseInt(String);
    } catch (NumberFormatException | NullPointerException nfe) {
        return false;
    }
    return true;
}

public void DoStuff(String string){
    if (isNumeratic(string.substring(1)) //ommits first
    {
      ///yourstuff
    }
}

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

相关问题 如何验证文本框中的字符串仅包含2个字符和3个数字? - How to validate that a string from a textbox contains only 2 characters and 3 numbers? 如何通过添加仅包含 0 和 1 的数字来生成字符串 - How to generate a string by adding the numbers that only contains 0 & 1 仅提取第一次出现的字符和连字符前后的所有数字 - Extract only first occurrence of character and all the numbers before and after hyphen 验证字符串是否只有数字 - Validate if string has only numbers 如何使字符串在数字之间包含“:”仅避免在任何字符 java 之前或之后使用“:”? - How to make a String include the ':' in between numbers only avoid the ':' before or after any character java? 如何检查字符串是否只包含字母、数字和双反斜杠 - How to check if string contains only letters, numbers and double backslash Java:如何检查字符串是否仅包含数字和连字符 - Java: How to check if a string contains only numbers and hyphens 验证字符串仅包含Java中的某些字符 - Validate a string contains only certain characters in java 我如何验证字符串仅使用数字并输出一条消息,说它必须仅是Bluej上的数字 - How do I validate a string to only use numbers and output a message saying it has to be only numbers on Bluej 当且仅当字符串包含不包含空格的字符时 - If and only if a string contains a character not including spaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM