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;
}
上一篇:postgresql获得max_parallel_workers是否能添加的状态(小试牛刀)


下一篇:Ubuntu初见