Results 1 to 4 of 4
- 02-07-2008, 10:28 PM #1
Member
- Join Date
- Feb 2008
- Posts
- 12
- Rep Power
- 0
Need a little help with a function!
Hi,
I'v got a slight problem, Im sure its quite simple to solve - I just dont know how to order the syntax :/
I have to solve the following problem:
Design, code and test a function:
public static boolean contains(String str1,String str2)
// pre: str1 and str2 not null
// post: returns true if str2 is a substring of str1.
Hint: you will need a loop, and will need to know the length of str1 and str2.
For the solution I have so far worked out that I need to make use of the Pattern and Matcher methods to determin if str2 (in its entirity) exists anywhere within str1. Any assistance would be greatly apprechiated!
- 02-07-2008, 10:44 PM #2
Easy solution
Welcome, Nuluvius, to Java Forums :D
There is a very easy solution for your problem. You can use the String.indexOf() method to determine if a String is a substring of another. I did this:
In my testing class, I had:Java Code:public static boolean contains(String string1, String string2){ return -1 != string1.indexOf(string2); }
output:Java Code:package pack; import java.util.*; public class Test{ public Test(){ System.out.println("Test running"); System.out.println("Enter string1:"); String string1 = Tools.readln(); System.out.println("Enter string2:"); String string2 = Tools.readln(); System.out.println("Result = " + contains(string1, string2)); } public static boolean contains(String string1, String string2){ return -1 != string1.indexOf(string2); } }
I hope this helps you. ;)Java Code:Test running Enter string1: true love Enter string2: love Result = true
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 02-07-2008, 11:07 PM #3
Member
- Join Date
- Feb 2008
- Posts
- 12
- Rep Power
- 0
Firstly, thank you for the welcome!
Secondly, that is amazing, I had actualy spent about 2/3 hours on this particular problem.. yet it was so simple. Thank you very much for sloving it and putting me out of my angush.
I think it goes without saying that I am only just learning java lol. This is my first year of a Software Engineering degree. I shal definatly be returning with my future conundrems :D
- 02-07-2008, 11:33 PM #4
Glad to help
I'm glad to help Nuluvius. I am currently studying a B.Sc. in Computer Science and I believe that your instructor either wants you to formulate an algorithm for this problem or use regular expressions. Anyway, good luck! :D
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
Similar Threads
-
use of onclick function
By m4tt in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:03 AM -
How to use a function in java
By olikhvar in forum New To JavaReplies: 2Last Post: 02-16-2008, 02:57 AM -
I want to add function
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:25 AM -
function name
By osval in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 08:56 PM -
Help with the Dummy Function
By Felissa in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-06-2007, 05:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks