设计一Test2类,在其main()方法内: (1)创建一空的StringBuilder对象sb (2)依次将”http://www.shop.com/query.do”,”?”,”key=java&page=2”附加到sb (3)将sb转换成String对象str (4)在str中查找’?’的索引,截取该索引左边的子串赋给String型的变量uri, 截取该索引右边的子串赋给String型的变量queryString (5)依次在控制台输出str,uri和queryString
时间: 2023-06-20 21:03:37 浏览: 70
下面是一个可能的Test2类实现:
```java
public class Test2 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("http://www.shop.com/query.do");
sb.append("?");
sb.append("key=java&page=2");
String str = sb.toString();
int index = str.indexOf('?');
String uri = str.substring(0, index);
String queryString = str.substring(index + 1);
System.out.println("str: " + str);
System.out.println("uri: " + uri);
System.out.println("queryString: " + queryString);
}
}
```
执行该程序,将会输出以下内容:
```
str: http://www.shop.com/query.do?key=java&page=2
uri: http://www.shop.com/query.do
queryString: key=java&page=2
```
阅读全文