60

次のような文字列がある場合:

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

また:

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

私のプログラムが文が有効であり、「仕事」を持っていることを私に与えるべきであるよりも。


私が持っている場合:

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

また:

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

その場合、私のプログラムは文に「仕事」があることを教えてはいけません。


だから私はJava文字列を見て、文の最後に.または,それの/前にある単語を見つけます。どうすればこれを達成できますか?

4

6 に答える 6

94

これは本当に簡単です。StringオブジェクトにはendsWithメソッドがあります。

/あなたの質問から、あなたはどちらか、,または.区切り文字セットとして欲しいようです。

それで:

String str = "This.is.a.great.place.to.work.";

if (str.endsWith(".work.") || str.endsWith("/work/") || str.endsWith(",work,"))
     // ... 

matchesメソッドとかなり単純な正規表現を使用してこれを行うこともできます。

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

[.,/]ピリオド、スラッシュ、またはコンマのいずれかを指定する文字クラスと、\1見つかった代替のいずれかに一致する後方参照を使用します(存在する場合)。

于 2012-09-07T02:40:17.780 に答える
53

次のように、文字列がworkで終わり、その後に1文字が続くかどうかをテストできます。

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

末尾の文字がオプションの場合は、次を使用できます。

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

最後の文字がピリオド.またはスラッシュであることを確認するには、/次を使用できます。

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

オプションのピリオドまたはスラッシュが続く作業をテストするには、次を使用できます。

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

ピリオドまたはスラッシュで囲まれた作業 をテストするには、次のようにします。

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

作業の前後 のトークンが互いに一致する必要がある場合は、次のようにすることができます。

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

正確な要件は正確に定義されていませんが、次のようになると思います。

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

言い換えると:

  • 0文字以上
  • 仕事が続く
  • ゼロまたは1つのORが続く, . /
  • 入力の終わりが続きます

さまざまな正規表現アイテムの説明:

.               --  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"

試してみるテストハーネスは次のとおりです。

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);
    }

}
于 2012-09-07T02:49:05.603 に答える
2

もちろん、StringTokenizerクラスを使用して文字列を「。」で分割することもできます。または「/」を入力し、最後の単語が「work」かどうかを確認します。

于 2012-09-07T02:42:26.667 に答える
2

部分文字列メソッドを使用できます。

   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);
于 2012-09-07T03:13:00.240 に答える
0
    String input1 = "This.is.a.great.place.too.work.";
    String input2 = "This/is/a/great/place/too/work/";
    String input3 = "This,is,a,great,place,too,work,";
    String input4 = "This.is.a.great.place.too.work.hahahah";
    String input5 = "This/is/a/great/place/too/work/hahaha";
    String input6 = "This,is,a,great,place,too,work,hahahha";
    
    String regEx = ".*work[.,/]";
    
    System.out.println(input1.matches(regEx)); // true
    System.out.println(input2.matches(regEx)); // true
    System.out.println(input3.matches(regEx)); // true
    System.out.println(input4.matches(regEx)); // false
    System.out.println(input5.matches(regEx)); // false
    System.out.println(input6.matches(regEx)); // false
于 2020-12-28T02:26:36.620 に答える