-3

I wanna build a function that will check a string whether it's valid format.

Input is like that:

String str1 = "Chapter 1: Home"

String str2 = "Chapter 2: Revenge"

String str3 = "Chapter 3c: Return"

The function will return true, if string is formated as "Chapter" + [a space] + [number] + ":" and return false if it's not

Output:

True
True
False

Edit: Thank you so much for your quick responds I have realized that Regex is the main point.

user3806875
  • 19
  • 1
  • 4

4 Answers4

1

You should try using regex.

Pattern.compile("^Chapter \\d+:.*").matcher(str).find();

Strikeskids
  • 3,932
  • 13
  • 27
1

Try this:

public static boolean isValid(String str) {
    return str.matches("Chapter \\d:.*");
} 
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Please also explain the code you submit to be more educative. – László Papp Jul 05 '14 at 03:33
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – László Papp Jul 05 '14 at 03:33
  • In addition, it is not worth feeding such poor questions with answers IMHO. – László Papp Jul 05 '14 at 03:50
  • @FinalContest actually it does answer the question, and is in fact a more succinct version of the accepted answer. As for explanation, the code is so straightforward and trivial it isn't worth explaining – Bohemian Jul 05 '14 at 03:55
  • 1
    You gave some "random" code that people do in comments. However, you did not really explain the comment, and that is why it did not reach the answer level in my values. But let us assume it is a number one answer on SO for a bit, you are still feeding a "gimme teh codez" question with answer, thus encouraging it to ask such question next time, etc. If I had the power, you have, I would just close the question... – László Papp Jul 05 '14 at 03:59
  • @FinalContest but my code is the entire method. nothing has been left out. I've wrapped the line in a method - maybe that will be clearer – Bohemian Jul 05 '14 at 04:03
  • 2
    @FinalContest Regarding the quality of the question, OP is clearly a noob and it is clear enough to answer. I prefer to help noobs - call it a weakness... – Bohemian Jul 05 '14 at 04:06
  • OK, well, I appreciate your willingness to help people who deserve it, but I think we need to disagree whether this type of question deserves it. Let it be so; it is not a big deal for me. I retracted my downvote. – László Papp Jul 05 '14 at 16:40
0

Look up String.matches(String regex)

0

You can use regex to test if the string matches a certain pattern. The regex could be this:

^Chapter \\d+: .*
  • ^ means it has to start with this string
  • \\d+ means it has to be one or more digits in that place
  • .* any more characters for 0 or more times

And then the function would be this (using the String#matches)

public static boolean validChapter (String s) {    
    return s.matches("^Chapter \\d+: .*");
}

Here are some tests:

public static void main(String[] args) {

    String str1 = "Chapter 1: Home";
    String str2 = "Chapter 2: Revenge";
    String str3 = "Chapter 3c: Return";
    String str4 = "Chapter 1:Return";

    System.out.println(validChapter(str1)); // true
    System.out.println(validChapter(str2)); // true
    System.out.println(validChapter(str3)); // false
    System.out.println(validChapter(str4)); // false
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97