• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using charAt() vs substring()

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On an assignment, I had a string where I wanted to extract a character and store it in another string. It was something like:
String s = "12345" ;
String part ;
part = s.substring( 0, 1 ) ;
The above worked. Now, suppose I wish to us charAt() instead of substring()? I tried:
String s = "12345" ;
String part ;
part = s.charAt( 0 ) ;
When I compile the above, it errors at assigning a char to a string. So, how do I fix this? Is it more efficient to use charAt() and convert it to a string, or just use substring()?
thanks,
-Bryan
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a bit hard to answer this without a bit of context. You indicate that you only want a single character, but you want to store it in a String -- why? If whatever uses this single character can simply be recoded to accept a char rather than a String, that would be the most efficient method.
Other than that, the speed difference between String.substring() and makeing a new String from the result of charAt() is probably negligible.
What is important, though is how the code makes sense to someone reading it. If (for example) the single character is an important system concept, and will only ever be a single character, using charAt makes that explicit. On the other hand, if in other similar cases it might be more than one character in length, then use substring() to make that explicit.
I strongly recommed this approach as a whole. Write all your code first for a human reader/maintainer, and only consider efficiency if it becomes important. A program which doesn't do what anyone wants, and can't be modified to do so, is a pretty useless program. Even if it does what it does very fast indeed.
 
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He wants to take the value of the the single digit and use it to index an array.
One way to get a substring and then use Integer.parseInt() to get the value of the first digit.
An alternative would involve using the charAt() method.
Without spelling out exactly what steps one would take to reach this end, shat would you recommend for this, Frank?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I've seen your issue with charAt() method and this is what you can do.

 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Alex! However, since this question was asked over 14 years ago, it's likely the OP has already found a solution.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question was about assigning the single char to a String.
 
reply
    Bookmark Topic Watch Topic
  • New Topic