DrJonOsterman
DrJonOsterman

Reputation: 139

C++ How do I turn the length of a string to an integer

So, if I have a string x, and x.length returns the count of characters in it,

how do I turn the return of string.length into an int?

I looked for duplicate questions but I don't think there are.

Thanks

Upvotes: 1

Views: 1492

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490058

The cast has already pointed out. Now, let me advise against using it, at least as a rule.

There's a reason it returns an unsigned integer type (among other things, because it might overflow what a signed integer can hold). You normally want to keep it as an unsigned type instead of converting it to int.

Upvotes: 2

Andrew Tomazos
Andrew Tomazos

Reputation: 68598

Just cast it to an int....

  int(x.length())

Upvotes: 7

Related Questions