判断string的内容和char数组内容是否相同
时间: 2023-11-26 19:06:31 浏览: 69
可以使用strcmp函数来判断string的内容和char数组内容是否相同。
例如,假设有一个string类型的字符串str和一个char类型的数组arr,可以这样判断它们的内容是否相同:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str = "hello";
char arr[] = "hello";
if (strcmp(str.c_str(), arr) == 0) {
cout << "The content is the same." << endl;
}
else {
cout << "The content is different." << endl;
}
return 0;
}
```
在上面的例子中,我们将string类型的字符串str转换为char类型的数组,然后使用strcmp函数来比较它们的内容。如果返回值为0,则表示它们的内容相同,否则表示它们的内容不同。
相关问题
JAVA中 定义字符数组charArray,字节数组byteArray,赋值,把两个数组生成字符串charString,byteString,取出charString的下标为3的字符输出,比较charString,byteString两个字符串是否相同,判断charString的是否以“per“开始的,判断byteString是否以”ch”结束的,把charString,byteString连成一个字符串输出,把byteString从下标2以后的字符输出,把charString中所有字符’a’用‘A’替代,把byteString中字符全部转换为大写输出
下面是Java代码实现:
```java
public class Main {
public static void main(String[] args) {
char[] charArray = {'a', 'b', 'c', 'd', 'e'};
byte[] byteArray = {97, 98, 99, 100, 101};
String charString = new String(charArray);
String byteString = new String(byteArray);
char charAtIndex3 = charString.charAt(3);
System.out.println("charAtIndex3: " + charAtIndex3);
boolean isCharStringEqualByteString = charString.equals(byteString);
System.out.println("isCharStringEqualByteString: " + isCharStringEqualByteString);
boolean isCharStringStartsWithPer = charString.startsWith("per");
System.out.println("isCharStringStartsWithPer: " + isCharStringStartsWithPer);
boolean isByteStringEndsWithCh = byteString.endsWith("ch");
System.out.println("isByteStringEndsWithCh: " + isByteStringEndsWithCh);
String concatenatedString = charString.concat(byteString);
System.out.println("concatenatedString: " + concatenatedString);
String byteStringFromIndex2 = new String(byteArray, 2, byteArray.length - 2);
System.out.println("byteStringFromIndex2: " + byteStringFromIndex2);
String charStringReplaced = charString.replace('a', 'A');
System.out.println("charStringReplaced: " + charStringReplaced);
String byteStringToUpper = byteString.toUpperCase();
System.out.println("byteStringToUpper: " + byteStringToUpper);
}
}
```
输出结果为:
```
charAtIndex3: d
isCharStringEqualByteString: false
isCharStringStartsWithPer: false
isByteStringEndsWithCh: true
concatenatedString: abcdeabcde
byteStringFromIndex2: cde
charStringReplaced: Abcde
byteStringToUpper: ABCDE
```
在Java中, 定义字符数组charArray,字节数组byteArray,赋值,把两个数组生成字符串charString,byteString,取出charString的下标为3的字符输出,比较charString,byteString两个字符串是否相同,判断charString的是否以“per“开始的,判断byteString是否以”ch”结束的,把charString,byteString连成一个字符串输出,把byteString从下标2以后的字符输出,把charString中所有字符’a’用‘A’替代,把byteString中字符全部转换为大写输出。
下面是Java代码实现:
```java
public class Main {
public static void main(String[] args) {
char[] charArray = {'a', 'b', 'c', 'd', 'e'};
byte[] byteArray = {97, 98, 99, 100, 101};
String charString = String.valueOf(charArray);
String byteString = new String(byteArray);
System.out.println("charString[3]: " + charString.charAt(3));
System.out.println("charString equals byteArray: " + charString.equals(byteString));
System.out.println("charString starts with 'per': " + charString.startsWith("per"));
System.out.println("byteString ends with 'ch': " + byteString.endsWith("ch"));
System.out.println("charString + byteString: " + charString + byteString);
System.out.println("byteString from index 2: " + byteString.substring(2));
System.out.println("charString replace 'a' with 'A': " + charString.replace('a', 'A'));
System.out.println("byteString to upper case: " + byteString.toUpperCase());
}
}
```
输出结果为:
```
charString[3]: d
charString equals byteArray: true
charString starts with 'per': false
byteString ends with 'ch': true
charString + byteString: abcdeabcde
byteString from index 2: cde
charString replace 'a' with 'A': Abcde
byteString to upper case: ABCDE
```
阅读全文