C++ Convert from 1 Char to String

C++ convert from 1 char to string?

All of

std::string s(1, c); std::cout << s << std::endl;

and

std::cout << std::string(1, c) << std::endl;

and

std::string s; s.push_back(c); std::cout << s << std::endl;

worked for me.

Convert a single character to a string?

Off the top of my head, if you're using STL then do this:

string firstLetter(1,str[0]);

Convert a char * [] to a string in C

You'll need to iterate through your pointers and concatenate them. You'll also want to be very careful not to overflow your target buffer.

A naive implementation that doesn't do bounds checking would look something like this:

char *ptr = s; // set ptr to the start of the destination buffer
for (i=0; i<number_of_pointers; i++) {
char *current_arg = args[i];
char c;
while ( (c = *current_arg++) ) {
// copy each character to the destination buffer until the end of the current string
*ptr++ = c;
}
*ptr++ = ' '; // or whatever joining character you want
}
*ptr = '\0'; // null terminate

You could also loop calls to strcat but you'll quickly run into Schlemiel the Painter.

convert a char* to std::string

std::string has a constructor for this:

const char *s = "Hello, World!";
std::string str(s);

Note that this construct deep copies the character list at s and s should not be nullptr, or else behavior is undefined.

How to convert an int to string in C?

EDIT: As pointed out in the comment, itoa() is not a standard, so better use sprintf() approach suggested in the rivaling answer!


You can use itoa() function to convert your integer value to a string.

Here is an example:

int num = 321;
char snum[5];

// convert 123 to string [buf]
itoa(num, snum, 10);

// print our string
printf("%s\n", snum);

If you want to output your structure into a file there is no need to convert any value beforehand. You can just use the printf format specification to indicate how to output your values and use any of the operators from printf family to output your data.

Is this the correct way to convert a char to a String in Java?

The shortest solution:

char c = 'a';
String s = "" + c;

The cleanest (and probably most efficient1) solutions:

char c = 'a';

String s1 = String.valueOf(c);
// or
String s2 = Character.toString(c);

Compared to your approach, the above prevents the overhead of having to box the primitive into a Character object.


1 The slight inefficiency of the string concatenation in the first approach might be optimized away by the compiler or runtime, so one (if one were so inclined) would really have to run a micro-benchmark to determine actual performance. Micro-optimizations like this are rarely worth the effort, and relying on concatenation to force string conversion just doesn't look very nice.

Convert char array of C into string of C++

The first thing you should change in the class name in your struct because that will likely cause problems because it is a keyword in C++.

Then for converting a C string into a C++ one, you can simply use the std::string constructor that accepts C-style strings.

std::string myCppString(MyStruct.name);

Pulling a single char from a string and converting it to int

You can write:

std::string s = "#/5";
std::string substring = s.substr(2, 1);
int value = std::stoi(substring);

Using the substr method of std::string to pull out the substring that you want to parse as an integer, and then using stoi (which takes a std::string) instead of atoi (which takes a const char *).

How to change 1 char in the string?

I found a solution in unsafe context:

    string str = "gg"; char c = 'H'; int index = 1;
fixed (char* arr = str) arr[index] = 'H';
Console.WriteLine(str);

It's so simple.
And in safe context:

    string str = "gg"; char c = 'H'; int index = 1;
GCHandle handle = GCHandle.Alloc(str, GCHandleType.Pinned);
IntPtr arrAddress = handle.AddrOfPinnedObject();
Marshal.WriteInt16(arrAddress + index * sizeof(char), c);
handle.Free();
Console.WriteLine(str);


Related Topics



Leave a reply



Submit