string.empty()
时间: 2024-04-17 18:07:53 浏览: 12
string.empty() 是一个 STL (标准模板库) 函数,用于检查字符串是否为空。如果字符串为空,则返回 true,否则返回 false。以下是示例代码:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str1 = "";
std::string str2 = "hello";
// 检查字符串是否为空
if (str1.empty()) {
std::cout << "str1 is empty" << std::endl;
} else {
std::cout << "str1 is not empty" << std::endl;
}
if (str2.empty()) {
std::cout << "str2 is empty" << std::endl;
} else {
std::cout << "str2 is not empty" << std::endl;
}
return 0;
}
```
输出结果为:
```
str1 is empty
str2 is not empty
```
相关问题
string.empty
As an AI language model, I assume you are asking about the C# property `string.Empty`.
`string.Empty` is a static property that returns an empty string. It is equivalent to `""`. It is commonly used to check for empty strings or to initialize a string variable as empty.
For example, instead of writing `string myString = "";`, you can write `string myString = string.Empty;`. This can make your code more readable and reduce the potential for errors.
You can also use it to check if a string is empty. For example, `if (myString == string.Empty)` or `if (string.IsNullOrEmpty(myString))` would both check if `myString` is empty.
string.Empty
在C#中,string.Empty是一个静态只读字段,表示一个空字符串,而""是一个字符串字面量,也表示一个空字符串。虽然它们在大多数情况下可以互换使用,但是使用string.Empty更加高效,因为它不需要分配存储空间,而""会分配一个长度为空的存储空间。因此,为了提高代码的性能和可读性,建议使用string.Empty。