If I have a string.

String msg = "I love mickey mouse";

What method can I used to display the string, "mickey"?

Recommended Answers

All 9 Replies

Looking at the Javadoc for the String class and finding the required method would be a good exercise for you.

I will use indexOf(). Is it correct?

Try to read documentation for the method public String substring(int beginIndex, int endIndex)

我会用 String[] split(String regex) 得到一个关于这个字符串的字符串数组

public String getStringMickey(){
       String msg = "I love mickey mouse"; 
       String[] str = msg.split(" ");
       return str[2];
}

subString

我会用 String[] split(String regex) 得到一个关于这个字符串的字符串数组

public String getStringMickey(){
       String msg = "I love mickey mouse"; 
       String[] str = msg.split(" ");
       return str[2];
}

Why str[2]?

Read the API doc for the split() method.
Where (what index) in the array returned by split() would the desired word be?

不知道楼上的意思是什么,回答引用人的问题:
String str[] = msg.split(" ");
这里的str是一个String数组,里面装的是{"I","love","mickey","mouse"}
now, do u know why str[2] 了吗?

Use subString() method...
See the documentation for method signature...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.