Solved: Get Number of Characters in Std::String

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

doctorzeus

Thread Starter
Joined
Jun 10, 2010
Messages
277
Hello,

The Title pretty much explains it but I need to know how to get the number of characters in a Std::string and ouput it as a integer e.g:

Code:
void main()
{

std::string str = "Cheesecake";
int NumOfChars;

/*
Get Number of characters in str and put the number in NumOfChars...
NumOfChars should then equal 9 (obviouslly starts counting from 0)

*/
(You can Probubly Guess I'm Using C/C++, VC++ to be specific)

Anyone know how to do this? Any Help Would be great...

Thanks

Doctorzeus
 

calvin-c

Banned
Joined
May 17, 2006
Messages
1,098
In VB.net a string is a character array so the Length property gives the number of characters. If that's not the case in C (I think it is but haven't used C for quite a few years) then you could parse the string in a loop, one character at a time, counting the number of characters until you reached the end of the string (terminated by a null character, isn't it?).
 

doctorzeus

Thread Starter
Joined
Jun 10, 2010
Messages
277
In VB.net a string is a character array so the Length property gives the number of characters. If that's not the case in C (I think it is but haven't used C for quite a few years) then you could parse the string in a loop, one character at a time, counting the number of characters until you reached the end of the string (terminated by a null character, isn't it?).
Thanks for the Reply

Yeah it is terminated by the 'NULL' character...

I'm not sure there is a length property included with string but I may be wrong, will look..

Thanks

Doctorzeus
 

doctorzeus

Thread Starter
Joined
Jun 10, 2010
Messages
277
In VB.net a string is a character array so the Length property gives the number of characters. If that's not the case in C (I think it is but haven't used C for quite a few years) then you could parse the string in a loop, one character at a time, counting the number of characters until you reached the end of the string (terminated by a null character, isn't it?).
Wow thanks,

That certanly was the right path,

looked up the string propertys and found the solution:

Code:
void main()
{

std::string str = "Cheesecake";
int NumOfChars;

NumOfChars = str.size();

*/
Can't believe it was that simple :) ;p

Thanks!

Doctorzeus
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top