有字符内容比较的方法吗?

char型的内容怎么比较内容呢?然后再将其全部连起来变成字符串类型

char 类型可以用==比较,连接成字符串的话,可以直接和一个空字符串相加就会自动拼接,也可以借助StringBuiler 去拼接

单个字符,直接 可以用 == 比较就行了
字符串比较 ,用 equals方法

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/170811
  • 你也可以参考下这篇文章:速度与压缩比如何兼得?压缩算法在构建部署中的优化
  • 除此之外, 这篇博客: 对构造函数的形参:其中使用char型变量和数组时,会出错,对出错类型的总结和解决方法中的 标题对构造函数的形参:其中使用char型变量和数组时,会出错,对出错类型的总结和解决方法。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    例题参考《c++程序设计(第三版)》谭浩强编著,第256页第250页
    p256页,代码:

    #include<iostream>
    using namespace std;
    #include <string>
    class Student
    {
    public:
    	Student(int n, string nam, char s)
    	{
    		num = n;
    		name=nam;
    		sex = s;
    		cout << "Constructor called. " << endl;
    	}
    	~Student()
    	{
    		cout << "Destructor called. " << endl;
    	}
    	void display()
    	{
    		cout << "num: " << num << endl << "name: " << name << endl << "sex: " << sex << endl;
    	}
    private:
    	int num;
    	char name[10];
    	char sex;
    };
    int main()
    {
    	Student stud1(10010, "Wang_li", 'f');
    	stud1.display();
    	Student stud2(10011, "Zhang_fang", 'm');
    	stud2.display();
    	return 0;
    }
    

    注意程序的第7,10,24 行,在编译时,会出现以下错误:
    不可指定数组类型“char [10]”

    表达式必须是可修改的左值
    与结构体变量中的char类型的赋值基本一样(可以参考我之前的博客)。
    解决办法
    全部统一成string型变量

    #include<iostream>
    using namespace std;
    #include <string>
    class Student
    {
    public:
    	Student(int n, string nam, char s)
    	{
    		num = n;
    		name=nam;
    		sex = s;
    		cout << "Constructor called. " << endl;
    	}
    	~Student()
    	{
    		cout << "Destructor called. " << endl;
    	}
    	void display()
    	{
    		cout << "num: " << num << endl << "name: " << name << endl << "sex: " << sex << endl;
    	}
    private:
    	int num;
    	string name;
    	char sex;
    };
    int main()
    {
    	Student stud1(10010, "Wang_li", 'f');
    	stud1.display();
    	Student stud2(10011, "Zhang_fang", 'm');
    	stud2.display();
    	return 0;
    }
    

    这样就可以正常运行程序了。
    (虽然我暂时不知道原理是什么)

    p250页的程序:
    这是在构造函数的形参表中,char型变量前面有int型变量

    #include <iostream>
    using namespace std;
    class Student
    {
    public:
    	Student(int n, char s, char nam[]) :num(n), sex(s)
    	{
    		strcpy_s(name, nam);
    	}
    private:
    	int num;
    	char sex;
    	char name[20];
    };
    Student st1(10110, 'm', "Wang_li");
    
    

    注意程序的最后一行,系统会提示:
    “Student::Student(int,char,char [])”: 无法将参数 3 从“const char [8]”转换为“char []”
    解决办法:
    暂时还不知道。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^