• Post Reply Bookmark Topic Watch Topic
  • New Topic

finding an element from a String without using existing methods  RSS feed

 
Christiaan Thamm
Ranch Hand
Posts: 34
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys

How do you access an element from a String without using methods like charAt/toCharArray/subString etc? In other words how did java create those methods in the first place?

Please dont ask me why I want to do this, that's irrelevant!

Thanks
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christiaan Thamm wrote:

Please dont ask me why I want to do this, that's irrelevant!



That's in fact relevant Because if you don't have an answer to that, then you are just trying to waste your time trying to do something which doesn't make sense. Why don't you want to use the existing APIs?
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christiaan Thamm wrote:Hi guys

How do you access an element from a String without using methods like charAt/toCharArray/subString etc? In other words how did java create those methods in the first place?

Please dont ask me why I want to do this, that's irrelevant!

Thanks


If you need an answer for your homework, then it might be relevant

In any case, you can't find an element in a string without using SOME API methods. That's like saying "Here's a black box, tell me if xyz is inside without opening it".

If your project/assignment is to research string search algorithms, then you probably have to work on the level of character arrays. So if you get a string as parameter, you would at least need to transform the string to a character array with the toCharArray() method.

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christiaan Thamm wrote:In other words how did java create those methods in the first place?

Since they are methods of the String class, they have access to the internal state of the String (which is an underlying character array). But since that's private, you don't.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christiaan Thamm wrote:In other words how did java create those methods in the first place?

You can lookup the source code for class java.lang.String, if you want to know exactly how it works internally. It's in the src.zip file that you can find in your JDK installation directory.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christiaan Thamm wrote:How do you access an element from a String without using methods like charAt/toCharArray/subString etc? In other words how did java create those methods in the first place?

The two questions are not related; and the answer to your first question is: you can't. - At least you can't do it without calling at least one method in the String class.

As to 'how did java create those methods in the first place?' (and it's 'Java', not 'java', BTW); the answer is: it didn't; the designer of the String class did - and s/he did it in exactly the same way as you or I would: they wrote it.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!