编程语言
首页 > 编程语言> > C++ -string method - find, replace, size, append

C++ -string method - find, replace, size, append

作者:互联网

#include<iostream.h>
#include<string>

int main() // main function
{

	std::string greeting = "Hello"; // empty string: ""
	greeting += "!"; // string method
	cout << greeting[5] << endl; // !
	cout << greeting.length() << endl; // 6, string is a class, length is method

	return 0;
}

// new IDE
#include <iostream>
#include <string>

int main()
{
    std::string greeting;
    getline(std::cin, greeting); // input a string
    std::cout << greeting << std::endl;
    return 0;
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "Hello";
    std::cout << greeting.size() << std::endl; // length of a string
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "Hello";
    greeting += " there"; // string method
    std::cout << greeting.size() << std::endl;
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "Hello";
    greeting.append(" there"); // another way
    std::cout << greeting << std::endl;
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "Hello";
    greeting.insert(3, "    "); // hel    lo
    std::cout << greeting << std::endl;
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "Hello";
    greeting.insert(3, " ");
    greeting.erase(3, 1);
    std::cout << greeting << std::endl;
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "Hello";
    greeting.insert(3, " ");
    greeting.erase(3, 1);
    greeting.erase(greeting.size() - 1); // Hello
    std::cout << greeting << std::endl;
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "Hello";
    greeting.insert(3, " ");
    greeting.erase(3, 1);
    // greeting.erase(greeting.size() - 1);
    greeting.pop_back(); // same as above line
    greeting.replace(0, 4, "Heaven"); // change a string
    std::cout << greeting << std::endl;
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "What the hell?";
    greeting.replace(greeting.find("hell"), 4, "****"); // replace hell
    // greeting.replace(9, 4, "****");
    std::cout << greeting << std::endl;
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "What is up?";
    std::cout << greeting.substr(5, 2) << std::endl; // is
    std::cout << greeting.find_first_of("!") << std::endl; // a huge number: not found
    unsigned long x = -1;
    std::cout << x << std::endl; // same huge number
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "What is up?";
    std::cout << greeting.find_first_of("!") << std::endl; // a huge number: not found
    // npos == -1 is NOT FOUND
    if(greeting.find_first_of("!") == -1) std::cout << "NOT FOUND!" << std::endl;
}

#include <iostream>
#include <string>

int main()
{
    std::string greeting = "What is up.";
    if(greeting.compare("What is up.") == 0) std::cout << "equals!" << std::endl;
}

标签:std,include,string,greeting,C++,replace,main,cout
来源: https://blog.csdn.net/weixin_43012796/article/details/111150504