Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning lenght of a string

 
Raymond Owens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I take a given string and return the number of characters that the string has?

For example, "character" is made up of 9 individual characters. What line of code will examine a string and answer this for me?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the java.lang.String JavaDocs?
 
Raymond Owens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Have you looked at the java.lang.String JavaDocs?


codePointCount?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Length.
 
Raymond Owens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had already tried length (apparently in the wrong context) and couldn't make it work. When trying to use the less than operator, I get the error that this cannot be used with java.lang.string, int.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without seeing any code it's hard to know where you went wrong. For example, the following works fine:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic