• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about substring() logic  RSS feed

 
Ken Whitestone
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string that I want to break up into it's constituent parts and store them in a string array.

For example, if the value of the String variable "str" is "My string", I want the resulting array to be like this:


I'm thinking, in order to do this, I need a loop to iterate over each letter of the string and assign it to the next field of the array, which I imagine would look something like this:


Clearly, that doesn't work and will always throw a "StringIndexOutOfBoundsException" once it gets to the end of the string because you can't add 1 to the end of that field.


I imagine I can do something similar to the following:

...but is there a better way to use substring() and extract a single letter based on the current value of "index", but ensure that I don't run out of bounds in the process?


If there's another way of doing it, that's great. I would love to see it. But I'm mostly concerned with making sure I understand the logic behind substring().
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken Ww wrote:
Clearly, that doesn't work and will always throw a "StringIndexOutOfBoundsException" once it gets to the end of the string because you can't add 1 to the end of that field.


Your for loop syntax is off. You need to replace the commas with semicolons. The code you posted doesn't even compile for me. Once you fix that, your first example should work. For example,"cat".substring(2, 3) returns "t" even though there is no index of 3, so you shouldn't run into errors using substring(int, int) the whole time.

Also, the String class has a built in method called toCharArray that converts a String into an array of characters. If you really need an array of Strings, though, you could use toCharArray and then convert that to a String array.

 
binhua zhong
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
binhua zhong wrote:


Mwuahaha. Beat you to it!
 
Ken Whitestone
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:Your for loop syntax is off. You need to replace the commas with semicolons. The code you posted doesn't even compile for me.

Yeah, that's just a typo...sorry about that.

Joel Christophel wrote:Once you fix that, your first example should work. For example,"cat".substring(2, 3) returns "t" even though there is no index of 3, so you shouldn't run into errors using substring(int, int) the whole time.

Strange. Something else must have been off with my original code. It kept throwing the "StringIndexOutOfBoundsException" and point to that line ( strArray[i] = str.substring(i, i + 1); ) as the culprit. But you're right. I fixed the example code and it works exactly as I though it should. Thank you.

Joel Christophel wrote:Also, the String class has a built in method called toCharArray that converts a String into an array of characters. If you really need an array of Strings, though, you could use toCharArray and then convert that to a String array.


Thank you for this. I imagine it will come in very handy.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken Ww wrote:I have a string that I want to break up into it's constituent parts and store them in a string array.

Just to add to the excellent advice you've been given, I'm wondering why you want the result in a String array.

You know that each element is only going to be 1 character long so why not just use the char[] returned by toCharArray()? It seems to me like you might be overthinking this.

However, presuming they do need to be Strings, and if you like "clever" code, here's another way to do it that doesn't require a loop:
However, in order to find out how it works, you'll have to read up on regular expressions and, in particular "negative look-behind".

Even if you don't follow the above right now, I strongly urge you to read up on String.split(). It's a very useful method to know about.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!