promotion image of download ymail app
Promoted
Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

Java-Strings 2

Write a static method EndsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false.

For example,

EndsWith( "radar installation", "rad" )

returns

false

EndsWith( "radar installation", "installation" )

returns

true

EndsWith( "radar installation", "" )

returns

true

EndsWith( "", "a" )

returns

false

EndsWith( "", "" )

returns

true

Method:

....?

System.out.println( EndsWith( " ", " " ) );

Example 1:

System.out.println( EndsWith( " Enterprise-D", " D" ) );

Result: true

Example2:

System.out.println( EndsWith( " Voyager ", "ager " ) );

Result: true

Example3:

System.out.println( EndsWith( " Deep Space Nine", " nine" ) );

Result: false

Example4:

System.out.println( EndsWith( "Deep Space Nine ", "Deep Space Nineteen " ) );

Result: false

2 Answers

Rating
  • Xanver
    Lv 4
    1 decade ago
    Favorite Answer

    public class Y0640 {

    public static boolean EndsWith(String s1, String s2) {

    if (s2.length() == 0) return true;

    if (s2.length() > s1.length()) return false;

    while (s2.startsWith(" ")) {

    s2 = s2.substring(1);

    }

    for (int i = 1; i <= s2.length(); i++) {

    if (s1.charAt(s1.length()-i) != s2.charAt(s2.length()-i)) return false;

    }

    return true;

    }

    public static void main(String[] args) {

    System.out.println(EndsWith("radar installation", "rad"));

    System.out.println(EndsWith("radar installation", "installation"));

    System.out.println(EndsWith("radar installation", ""));

    System.out.println(EndsWith("", "a"));

    System.out.println(EndsWith("", ""));

    System.out.println(EndsWith(" Enterprise-D", " D"));

    System.out.println(EndsWith(" Voyager ", "ager "));

    System.out.println(EndsWith(" Deep Space Nine", " nine"));

    System.out.println(EndsWith("Deep Space Nine ", "Deep Space Nineteen "));

    }

    }

    Source(s): me
    • Commenter avatarLogin to reply the answers
  • 1 decade ago

    請參考我的做法

    public class Y0640 { public static boolean EndsWith(String s1, String s2) { if (s2.length() == 0) return true; if (s2.length() > s1.length()) return false; while (s2.startsWith(" ")) { s2 = s2.substring(1); } for (int i = 1; i <= s2.length(); i++) { if (s1.charAt(s1.length()-i) != s2.charAt(s2.length()-i)) return false; } return true; } public static void main(String[] args) { System.out.println(EndsWith("radar installation", "rad")); System.out.println(EndsWith("radar installation", "installation")); System.out.println(EndsWith("radar installation", "")); System.out.println(EndsWith("", "a")); System.out.println(EndsWith("", "")); System.out.println(EndsWith(" Enterprise-D", " D")); System.out.println(EndsWith(" Voyager ", "ager ")); System.out.println(EndsWith(" Deep Space Nine", " nine")); System.out.println(EndsWith("Deep Space Nine ", "Deep Space Nineteen ")); }}

    參考資料Java-Strings 2 問題

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.