File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to find a particular substring in a string without using any builtin methods of string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find a particular substring in a string without using any builtin methods of string" Watch "How to find a particular substring in a string without using any builtin methods of string" New topic
Author

How to find a particular substring in a string without using any builtin methods of string

amit kumar goyal
Greenhorn

Joined: Dec 07, 2006
Posts: 10
Hi my question is how can we find a particular string in another string without using any inbuilt String class methods. for eg. string "cofee" inside "please give me a cup of cofee". Thanks
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

What do you mean
amit kumar goyal wrote:how can we find a particular string in another string without using any inbuilt String


Java Best Practices
Linux Best Practices
Amortization Calculator

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9319
    
  17

Hi Amit. Welcome to javaranch.

Well for that first you'll have to convert the string into a char array using, well a built in method of string class i.e. toCharArray and then implement a simple algorithm to search the char array. Or you can use the charAt method to check the characters at different indexes of the string and match it with corresponding characters of your other string. In both the cases you will have to use methods of the string class. I could have given you the exact code but here at ranch, we encourage people to try to solve simple problems like this on their own...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
amit kumar goyal
Greenhorn

Joined: Dec 07, 2006
Posts: 10
means does that string "cofee" or for that matter any string is there in another string.......i hope you got that...
amit kumar goyal
Greenhorn

Joined: Dec 07, 2006
Posts: 10
Thanks for the reply....But i want a solution where we do not use any of the String class built in methods...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

amit kumar goyal wrote:Thanks for the reply....But i want a solution where we do not use any of the String class built in methods...


Well, if you can't call *any* methods of the string class, then I believe it is *not* possible, as you won't be able to extract the data. Heck, to print the string to the screen, the internal libraries even use some of the methods.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

amit kumar goyal wrote:Thanks for the reply....But i want a solution where we do not use any of the String class built in methods...


why you want to do this? you want solution ? then how about this ?

amit kumar goyal
Greenhorn

Joined: Dec 07, 2006
Posts: 10
I was asked this question in an interview.....Probably they want some solution like how it is implemented in the String class....the also asked a question like how can we find the length of a String without using any inbuilt methods......
amit kumar goyal
Greenhorn

Joined: Dec 07, 2006
Posts: 10
Anyways thanks everyone for the help......
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

amit kumar goyal wrote:I was asked this question in an interview.....Probably they want some solution like how it is implemented in the String class....the also asked a question like how can we find the length of a String without using any inbuilt methods......


I think you are reading the question too strictly. I agree that they probably want some solution on how it is implemented -- which you should have answered... that is it held internally as a char array, and that you can get a copy of that array, using the toCharArray() method. I am sure that they don't mean that you can't call that method.

Then to answer the substring question, you need to talk about loops and checking characters.etc. For your second question, it is simply the length of the array.

Henry


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

As said before, you need at least toCharArray() or a combination of length() and charAt(int). Those are the only options to be able to inspect the contents of the strings.

Personally I would use length() and charAt(int) since it doesn't create a new char[].


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find a particular substring in a string without using any builtin methods of string