1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Get Number of Characters in Std::String

Discussion in 'Software Development' started by doctorzeus, Jan 21, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. doctorzeus

    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
     
  2. calvin-c

    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?).
     
  3. doctorzeus

    doctorzeus Thread Starter

    Joined:
    Jun 10, 2010
    Messages:
    277
    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
     
  4. doctorzeus

    doctorzeus Thread Starter

    Joined:
    Jun 10, 2010
    Messages:
    277
    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
     
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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/976020

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice