plz HELP! Basic JAVA Programming ---3 parts needed

来源:百度知道 编辑:UC知道 时间:2024/09/27 10:27:08
/**
* Method getHappyBirthdayName
* Input: any happy birthday song that follows the pattern:
* "Happy birthday to you, happy birthday to you, happy birthday dear XXXX, happy birthday to you"
* where XXXX is replaced with a name
* Returns: the name used in the song
*
* Examples:
* Input: "Happy birthday to you, happy birthday to you, happy birthday dear Superman, happy birthday to you"
* Returns: "Superman"
*
* Input: "Happy birthday to you, happy birthday to you, happy birthday dear Captain Jack Sparrow, happy birthday to you"
* Returns: "Captain Jack Sparrow"
*
* Input: "Happy birthday to you, happy birthday to you, happy birthday dear Luke Skywalker, happy birthday to you"
* Returns: "Luke Skywalker"
*
*/
/*
* HINT

Your prof means you to use String.substring()
The code has been tested.

public String getHappyBirthdayName(String HappyBirthdayName) {
return HappyBirthdayName.substring(66, HappyBirthdayName.length()-23);
}

public String getDomainName(String emailAddress) {
return emailAddress.substring(emailAddress.indexOf('@')+1);
}

public String getMovieTitle(String extendedMovieTitle) {
return extendedMovieTitle.substring(0, extendedMovieTitle.indexOf(':'));
}

使用String中的 split()方法;利用正则表达式把字符串切开。

具体代码如下:
public class ABC {

/*
* main for test
*/
public static void main(String args[]) {
ABC abc = new ABC();

String song ="Happy birthday to you, happy birthday to you, happy birthday dear XXXX, happy birthday to you";
String s0 = abc.getHappyBirthdayName(song);
System.out.println("happyBirthdayName is :