-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_starts_ends_demo.cpp
69 lines (48 loc) · 2.74 KB
/
string_starts_ends_demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// Demos MSVC STL implementation for https://wg21.link/P0457R2
#include <iostream>
#include <string_view>
#include <string>
using namespace std::literals;
void string_demo()
{
std::cout << "std::string:\n";
const std::string cpp_demo{ "C++ 20 Demo" };
std::string search_string{ "C++" };
std::cout << "\"" << cpp_demo << "\" starts with \"" << search_string << "\": " << cpp_demo.starts_with(search_string) << std::endl;
search_string = "Java";
std::cout << "\"" << cpp_demo << "\" starts with \"" << search_string << "\": " << cpp_demo.starts_with(search_string) << std::endl;
search_string = "Demo";
std::cout << "\"" << cpp_demo << "\" ends with \"" << search_string << "\": " << cpp_demo.ends_with(search_string) << std::endl;
search_string = "demo";
std::cout << "\"" << cpp_demo << "\" ends with \"" << search_string << "\": " << cpp_demo.ends_with(search_string) << std::endl;
std::cout << "\"" << cpp_demo << "\" starts with 'C': " << cpp_demo.starts_with('C') << std::endl;
std::cout << "\"" << cpp_demo << "\" starts with 'J': " << cpp_demo.starts_with('J') << std::endl;
const std::string cpp_demo_literal{ "My favorite programming language is\0\0C++"s };
std::cout << "\"" << cpp_demo_literal << "\" ends with \"Java\"s: " << cpp_demo_literal.ends_with("Java"s) << std::endl;
std::cout << "\"" << cpp_demo_literal << "\" ends with \"C++\"s: " << cpp_demo_literal.ends_with("C++"s) << std::endl;
}
void string_view_demo()
{
std::cout << "std::string_view:\n";
const std::string_view cpp_demo{ "This demo is for string_view" };
std::string search_string{ "This" };
std::cout << "\"" << cpp_demo << "\" starts with \"" << search_string << "\": " << cpp_demo.starts_with(search_string) << std::endl;
search_string = "That";
std::cout << "\"" << cpp_demo << "\" starts with \"" << search_string << "\": " << cpp_demo.starts_with(search_string) << std::endl;
search_string = "view";
std::cout << "\"" << cpp_demo << "\" ends with \"" << search_string << "\": " << cpp_demo.ends_with(search_string) << std::endl;
search_string = "something_else";
std::cout << "\"" << cpp_demo << "\" ends with \"" << search_string << "\": " << cpp_demo.ends_with(search_string) << std::endl;
std::cout << "\"" << cpp_demo << "\" starts with 'T': " << cpp_demo.starts_with('T') << std::endl;
std::cout << "\"" << cpp_demo << "\" starts with 'X': " << cpp_demo.starts_with('X') << std::endl;
}
void string_starts_ends_with_demo()
{
// Set boolalpha format flag so bool values are represented by their textual representation: true or false
std::boolalpha(std::cout);
std::cout << "\nstring starts_with(), ends_with() demo:\n";
string_demo();
string_view_demo();
}