Check if string ends with certain pattern

后端 未结 5 1859
予麋鹿
予麋鹿 2020-12-03 00:11

If I have a string like:

This.is.a.great.place.too.work.

or:

This/is/a/great/place/too/work/

than my prog

相关标签:
5条回答
  • 2020-12-03 00:50

    I tried all the different things mentioned here to get the index of the . character in a filename that ends with .[0-9][0-9]*, e.g. srcfile.1, srcfile.12, etc. Nothing worked. Finally, the following worked: int dotIndex = inputfilename.lastIndexOf(".");

    Weird! This is with java -version:

    openjdk version "1.8.0_131"
    OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-0ubuntu1.16.10.2-b11)
    OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
    

    Also, the official Java doc page for regex (from which there is a quote in one of the answers above) does not seem to specify how to look for the . character. Because \., \\., and [.] did not work for me, and I don't see any other options specified apart from these.

    0 讨论(0)
  • 2020-12-03 00:51

    Of course you can use the StringTokenizer class to split the String with '.' or '/', and check if the last word is "work".

    0 讨论(0)
  • 2020-12-03 01:00

    This is really simple, the String object has an endsWith method.

    From your question it seems like you want either /, , or . as the delimiter set.

    So:

    String str = "This.is.a.great.place.to.work.";
    
    if (str.endsWith(".work.") || str.endsWith("/work/") || str.endsWith(",work,"))
         // ... 
    

    You can also do this with the matches method and a fairly simple regex:

    if (str.matches(".*([.,/])work\\1$"))
    

    Using the character class [.,/] specifying either a period, a slash, or a comma, and a backreference, \1 that matches whichever of the alternates were found, if any.

    0 讨论(0)
  • 2020-12-03 01:00

    You can test if a string ends with work followed by one character like this:

    theString.matches(".*work.$");
    

    If the trailing character is optional you can use this:

    theString.matches(".*work.?$");
    

    To make sure the last character is a period . or a slash / you can use this:

    theString.matches(".*work[./]$");
    

    To test for work followed by an optional period or slash you can use this:

    theString.matches(".*work[./]?$");
    

    To test for work surrounded by periods or slashes, you could do this:

    theString.matches(".*[./]work[./]$");
    

    If the tokens before and after work must match each other, you could do this:

    theString.matches(".*([./])work\\1$");
    

    Your exact requirement isn't precisely defined, but I think it would be something like this:

    theString.matches(".*work[,./]?$");
    

    In other words:

    • zero or more characters
    • followed by work
    • followed by zero or one , . OR /
    • followed by the end of the input

    Explanation of various regex items:

    .               --  any character
    *               --  zero or more of the preceeding expression
    $               --  the end of the line/input
    ?               --  zero or one of the preceeding expression
    [./,]           --  either a period or a slash or a comma
    [abc]           --  matches a, b, or c
    [abc]*          --  zero or more of (a, b, or c)
    [abc]?          --  zero or one of (a, b, or c)
    
    enclosing a pattern in parentheses is called "grouping"
    
    ([abc])blah\\1  --  a, b, or c followed by blah followed by "the first group"
    

    Here's a test harness to play with:

    class TestStuff {
    
        public static void main (String[] args) {
    
            String[] testStrings = { 
                    "work.",
                    "work-",
                    "workp",
                    "/foo/work.",
                    "/bar/work",
                    "baz/work.",
                    "baz.funk.work.",
                    "funk.work",
                    "jazz/junk/foo/work.",
                    "funk/punk/work/",
                    "/funk/foo/bar/work",
                    "/funk/foo/bar/work/",
                    ".funk.foo.bar.work.",
                    ".funk.foo.bar.work",
                    "goo/balls/work/",
                    "goo/balls/work/funk"
            };
    
            for (String t : testStrings) {
                print("word: " + t + "  --->  " + matchesIt(t));
            }
        }
    
        public static boolean matchesIt(String s) {
            return s.matches(".*([./,])work\\1?$");
        }
    
        public static void print(Object o) {
            String s = (o == null) ? "null" : o.toString();
            System.out.println(o);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-03 01:07

    You can use the substring method:

       String aString = "This.is.a.great.place.too.work.";
       String aSubstring = "work";
       String endString = aString.substring(aString.length() - 
            (aSubstring.length() + 1),aString.length() - 1);
       if ( endString.equals(aSubstring) )
           System.out.println("Equal " + aString + " " + aSubstring);
       else
           System.out.println("NOT equal " + aString + " " + aSubstring);
    
    0 讨论(0)
提交回复
热议问题