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

String

 
abhinas raj
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s1="john,24,usa"; How to extract each substring from the given string without using any built-in Java methods of either String or StringBuffer/StringBuilder class ? please help
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without any of the methods in the String class it will be impossible, because there's no way to get at the contents of the string without calling any methods of the String class.
 
Ameya Kamat
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you be a little more clear about the question? what are the substrings you are trying to extract? is ',' a token if so you could use StringTokenizer. if the substrings are going to be a fixed length then you could create a inputreader object and read specific characters. I think it all comes down to what you define as substring
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you read the StringTokenizer (←link) documentation, you find you ought not to use it.
 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think the best way to do this is with the method String.split().
This method allows you to split a String in different pieces, using a delimiter that determines where the String is split.
These pieces, then, will be put in an array.

For example:


the output will be:

This
is
a
test

I hope this helped
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch , Kenneth van Gysegem

I think the first post means that String#split is not permitted. It is a daft exercise like that, anyway.
 
Kenneth Van Gysegem
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch , Kenneth van Gysegem

I think the first post means that String#split is not permitted. It is a daft exercise like that, anyway.


You're right. String.split() is a built in java-method.
And you are right about the exercise being daft too
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic