Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How do I parse a char array such as:
"2019-11-01T17:00:10.000Z"
I want 17:00:10.000 and nothing else. How do I parse with respect to the char array indices?
The simplest and safest way to deal with strings is to use the standard std::string. The std::string has many class member functions that can help with this. One of them is substr that can be used to create a new std::string from a part of a string.
Example:
#include <iostream>
#include <stdexcept>
#include <string>
std::string get_time_str(const std::string& timestamp) {
if(timestamp.size() != 24)
throw std::runtime_error("bad timestamp: "+ timestamp);
return timestamp.substr(11, 12); // start at pos 11 and 12 chars forward
}
int main() {
const char* ts = "2019-11-01T17:00:10.000Z";
std::cout << get_time_str(ts) << '\n';
}
Output:
17:00:10.000
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How can make a program typing with c++?
For example with (char) can type a grapheme but i want type word.
char x;
x=='c';
But i want type for example 'equation'.
Someone know how can i do this?
Use a std::string. You can also use char* but i recommend you the first one. Remember that string goes within "" instead of ''.
string x; //You have to define what is 'x'.
x=="equation";
If you use char*, it behaves as an array of chars.
#include <string> // paste this line on top of the file
std::string name = "Bob"; // Use double quotes here.
if(name == "Bob")
{
std::cout << name << std::endl; // works as you expected
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I wanted to ask,
how does string::string operator function, I know that it is a standard constructor, for using strings, yet what does operator do? Does it allow me to use the multiplier operator at the end? Size_t represents the size of an object and string& is a pass by reference. How are these concepts making sense?
#include <iostream>
#include <string>
using namespace std::literals::string_literals;
std::string operator*(std::size_t n, const std::string& s)
{
std::string ret;
while (n--)
ret += s;
return ret;
}
int main()
{
std::cout << 5 * std::string("Hallo") << std::endl;
std::cout << 5 * "Test"s << std::endl;
}
What does std::string ret mean, can I use it because of std::string? Because std::string has been defined at the beginning ?
By implementing operator*, you allow type size_t to be "multiplied" by type string. The reason multiplied is in quotes is because you implement yourself what "multiply" means. In this particular implementation, the string is just appended to itself n times.
So 5 * std::string("Hallo") will result in HalloHalloHalloHalloHallo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I just started learning C++.
I want to print out a sentence or at least a word.
I could just use cout<<"hello world";
but I want to use linked list.
Is that possible?
You could do it this way, which uses a linked list and output printing a word or space at a time...
#include <list>
#include <iostream>
#include <string>
int main() {
std::list<std::string> words;
words.push_back("hello");
words.push_back(" ");
words.push_back("world");
for (auto const& word : words) {
std::cout << word;
}
std::cout << std::endl;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to modify a c string in C++.
void modify(char* s)
{
s[0] = 'a';
}
If I do this, there will be some undefined behavior and can't run.
Let's assume s[0] is valid. I know char* s is immutable. Is there any possibility that I can modify the s[0] in place, which means, without creating a new string. Do the modification on the original string.
I think you might be misunderstanding some other answers you have seen on the web.
It is only undefined behavior to modify a string constant, not any char*.
As long as you strdup the constant string into a non-constant string, you can make whatever changes you want with it, because it is now in a mutable region of memory.
#include <stdio.h>
#include <string.h>
void modString(char* changeMe) {
changeMe[0] = 'g';
}
int main(){
char* foo = strdup("food");
puts(foo);
modString(foo);
puts(foo);
free(foo);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If the code is like the below,
void func(std::string str)
{
...
}
void main()
{
std::string p1 = "abcd";
char p2[SOME_LENGTH] = "abcd";
func(p1); // (1)
func(p2); // (2)
}
which way is efficient between (1) and (2)?
They are equally efficient/inefficient. Both involves copying the string and using the copy as the value of the argument 'str'. A better way would be declaring func as
void func(const std::string &str) {
}
This can avoid copying of the string.