Results 1 to 4 of 4
  1. #1
    Nuluvius is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Exclamation 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!

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default 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:
    Java Code:
    public static boolean contains(String string1, String string2){
    	return -1 != string1.indexOf(string2);
    }
    In my testing class, I had:
    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);
    	}
    }
    output:
    Java Code:
    Test running
    Enter string1:
    true love
    Enter string2:
    love
    Result = true
    I hope this helps you. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    Nuluvius is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Default

    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

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default 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

  1. use of onclick function
    By m4tt in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 04:03 AM
  2. How to use a function in java
    By olikhvar in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:57 AM
  3. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:25 AM
  4. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 09:56 PM
  5. Help with the Dummy Function
    By Felissa in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-06-2007, 06:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •