Sign in to follow this  
Jesbass

string-length Function

Recommended Posts

Jesbass    122
Hi there. I'm trying to ascertain the length of a std:string, (not a cstring), and the documentation suggests using the string-length function:
Quote:
Returns the number of characters in the string. number string-length(string) Remarks If the argument is omitted, it returns the number of characters in the string-value of the context node. The following function call returns 4: string-length("abcd") The following function call returns 5: string-length("ab cd") If an argument is not of type string, it is first converted to a string and then evaluated.
However, when I add the following piece of code:
Quote:
Int32 iStringLength = string-length(m_strTeamName);
I get the following errors:
Quote:
e:\Documents and Settings\David & Angela\Desktop\Rugby League Manager\Season.cpp(74): error C2065: 'string' : undeclared identifier e:\Documents and Settings\David & Angela\Desktop\Rugby League Manager\Season.cpp(74): error C3861: 'length': identifier not found, even with argument-dependent lookup
When, in an attempt to remove the first error, I try changing the code to:
Quote:
Int32 iStringLength = std::string-length(m_strTeamName);
I get the following errors:
Quote:
e:\Documents and Settings\David & Angela\Desktop\Rugby League Manager\Season.cpp(74): error C2275: 'std::string' : illegal use of this type as an expression e:\Documents and Settings\David & Angela\Desktop\Rugby League Manager\Season.cpp(74): error C3861: 'length': identifier not found, even with argument-dependent lookup
1) Is there something I've missed out, such as an include file? The documentation doesn't seem to indicate any required header files. :/ 2) Alternatively, is there another way to determine the length of a string? (Preferably a simple one, hehe!)

Share this post


Link to post
Share on other sites
Drew_Benton    1861
I'm not sure what all that string-length stuff is, because in C++, you cannot have a function with an identifier of string-length, because the - is not allowed for names. I'm thinking it's jsut saying to use the string's length function.

What you need is the std::string::size() function, which returns the length of your string or you can use the length() function, std::string::length(). More information here.

Share this post


Link to post
Share on other sites
Jesbass    122
Ah, fantastic! Thanks, guys - that fixed the problem! :)

Just in case anyone is curious, the correct piece of code is:

Quote:
Int32 iStringLength = m_strTeamName.length();


Thanks again! :)

EDIT: Actually, there was a minor casting warning, but that was fixed by:

Quote:
Int32 iStringLength = static_cast<Int32>(m_strTeamName.length());


:)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this