mess.substring
时间: 2024-06-13 18:07:29 浏览: 6
根据您提供的引用内容,似乎是Java中的字符串操作。`substring()`是Java中的一个字符串方法,用于从一个字符串中提取子字符串。该方法有两个参数,第一个参数是子字符串的起始位置,第二个参数是子字符串的结束位置(不包括该位置的字符)。如果只提供一个参数,则返回从该位置到字符串末尾的所有字符。以下是一个例子:
```java
String str = "Hello World";
String subStr1 = str.substring(0, 5); // 从0开始,到第5个字符(不包括第5个字符)
String subStr2 = str.substring(6); // 从第6个字符到字符串末尾
System.out.println(subStr1); // 输出:Hello
System.out.println(subStr2); // 输出:World
```
相关问题
将以下代码改错:public class FindMess { public static void main(String args[]) { String mess = "书名:Java程序设计,出版时间:2011.10.01,"+ "出版社:清华大学出版社,价格:29.8 元,页数:389页"; if(【代码1】) { //判断mess中是否含有"程序" System.out.println("图书信息包含有\"程序\""); } int index=【代码2】//mess调用indexOf(String s,int start)返回mess中第2个冒号的位置 String date = mess.substring(index+1,index+11); System.out.println(date); int pricePosition= 【代码3】//mess调用indexOf(String s)返回首次出现"价格"的位置 int endPosition=mess.indexOf("元"); String priceMess = mess.substring(pricePosition+3,endPosition); System.out.println("图书价格:"+priceMess); double price=Double.parseDouble(priceMess); if(price>=29) { System.out.println("图书价格"+price+"大于或等于29元"); } else { System.out.println("图书价格"+price+"小于29元"); } index = 【代码4】//mess调用laseIndexOf(String s,int start)返回最后一个冒号位置 endPosition=mess.lastIndexOf("页"); String pageMess = mess.substring(index+1,endPosition); int p = Integer.parseInt(pageMess); if(p>=360) { System.out.println("图书的页数"+p+"大于或等于360"); } else { System.out.println("图书的页数"+p+"小于360"); } } }
public class FindMess {
public static void main(String args[]) {
String mess = "书名:Java程序设计,出版时间:2011.10.01," +
"出版社:清华大学出版社,价格:29.8 元,页数:389页";
if(mess.contains("程序")) { //代码1:判断mess中是否含有"程序"
System.out.println("图书信息包含有\"程序\"");
}
int index = mess.indexOf(':', mess.indexOf(':') + 1); //代码2:mess调用indexOf(String s,int start)返回mess中第2个冒号的位置
String date = mess.substring(index + 1, index + 11);
System.out.println(date);
int pricePosition = mess.indexOf("价格"); //代码3:mess调用indexOf(String s)返回首次出现"价格"的位置
int endPosition = mess.indexOf("元");
String priceMess = mess.substring(pricePosition + 3, endPosition);
System.out.println("图书价格:" + priceMess);
double price = Double.parseDouble(priceMess);
if(price >= 29) {
System.out.println("图书价格" + price + "大于或等于29元");
} else {
System.out.println("图书价格" + price + "小于29元");
}
index = mess.lastIndexOf(':'); //代码4:mess调用lastIndexOf(String s,int start)返回最后一个冒号位置
endPosition = mess.lastIndexOf("页");
String pageMess = mess.substring(index + 1, endPosition);
int p = Integer.parseInt(pageMess);
if(p >= 360) {
System.out.println("图书的页数" + p + "大于或等于360");
} else {
System.out.println("图书的页数" + p + "小于360");
}
}
}
结构体和指针是如何运用的while (true)//判断是否进入游戏 { ExMessage mess; //结构体 getmessage(&mess, EM_MOUSE); //定义结构体指针 if (mess.lbutton) { if (PointInRect(mess.x, mess.y, tplayr)) //点击开始 { return; } else if (PointInRect(mess.x, mess.y, texitr)) //推出 { exit(0); //退出游戏
结构体和指针在上述代码中的运用如下:
1. 首先定义了一个名为`ExMessage`的结构体,该结构体的定义可能在代码的其他地方。
2. 在`while (true)`循环中,创建了一个名为`mess`的`ExMessage`类型的结构体对象。
3. 调用了`getmessage`函数,该函数接受一个指向`ExMessage`结构体的指针作为参数,将鼠标的消息信息填充到该结构体中。
4. 通过使用结构体对象的成员变量,判断鼠标左键是否被按下。
5. 如果鼠标左键被按下,则进一步判断鼠标点击的位置是否在`tplayr`所代表的矩形区域内。
6. 如果在矩形区域内,则返回,意味着进入游戏。
7. 如果不在矩形区域内,则判断鼠标点击的位置是否在`texitr`所代表的矩形区域内。
8. 如果在矩形区域内,则调用`exit(0)`函数退出游戏。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)