2.2 对于下列代码:String str1 = "AAA软件教育"; String str2 = "AAA软件教育"; String str3 = new String("AAA软件教育"); StringBuffer str4 = new StringBuffer("AAA软件教育"); 以下表达式返回true的是()。A.str1 == str2 B.str2 == str3 C.str1 =
时间: 2024-02-12 07:18:27 浏览: 27
下列表达式返回true的是A. str1 == str2 和 B. str2 == str3。因为str1和str2都指向字符串常量池中的同一个字符串对象,而str3指向堆内存中的一个新的字符串对象。虽然str4也指向堆内存中的新对象,但是StringBuffer类并没有重写equals方法,所以不会调用到StringBuffer类的equals方法,而是调用到Object类的equals方法,因此无论如何都会返回false。
相关问题
string str1 = "1.200000"; double aaa = 0.0; sscanf_s(str1.c_str(), "%.f", &aaa); cout << aaa;检查一下有什么问题
这段代码中存在一个问题。具体来说,在使用 `sscanf_s` 函数时,格式化字符串 "%.f" 会将浮点数按照整数来读取,因此会将字符串 "1.200000" 中的小数部分舍去,只读取整数部分 "1",最终输出的结果为 "1.000000"。如果想要正确地将字符串转换成浮点数,应该使用格式化字符串 "%lf",并将变量 `aaa` 的类型改为 `double`,即:
```
string str1 = "1.200000";
double aaa = 0.0;
sscanf_s(str1.c_str(), "%lf", &aaa);
cout << aaa;
```
这样才能正确地将字符串 "1.200000" 转换成浮点数 1.2 并输出。
"<aaa>".contains("<")为true,String str = "<aaa>"; str.contains("<")为什么为false
这是因为 `"<aaa>"` 中包含了尖括号 `<` 和 `>`,而这些符号在字符串中具有特殊的含义,即表示字符串的开始和结束。因此,当你使用 `str.contains("<")` 的时候,它实际上是在查找是否包含一个以 `<` 开始的子字符串,而不是单纯查找 `<` 这个字符。因为 `"<aaa>"` 中没有以 `<` 开始的子字符串,所以返回值为 false。如果你想要查找 `<` 这个字符,可以使用转义符 `\`,即 `str.contains("\\<")`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)