Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: First string is a substring of another

1. ## First string is a substring of another

I need to write a User Defined Method which
Checks if the first string is a substring of the second one.
- Returns 0 if it is not a substring / 1 if it is / -1 if it is a “circular substring”
this is an example of what it should do

- abcde is a substring of blkjabcdelkjsd  return 1
- abcde is not a substring of slfkjsdlskggfhjkh  return 0
- abcde is a “circular substring” of delkjlkjabc  return -1

Can Anyone help?

All I have is
```public static int substring(String Str1, String Str2)
{
index1 = Str2.indexOf(Str1)

if (index1 >= 0)
{
return 1;
else

}```

2. ## Re: First string is a substring of another

Here's a simple algorithm to check if a substring is contained within another string:

1. start from the beginning of the larger string
2. If the remaining characters is greater than the length of the second string, do 2a. Otherwise, do 2b
2a. Use the substring() method of the String class to take a chunk that has the same length as the smaller string
2b. Use the substring() method to take the rest of the letters in the larger string, then add on the remaining characters needed from the beginning
3. compare if they are equal using the equals() method. If they are equal, return 1 or -1, depending on whether you used 2a or 2b. Otherwise, repeat 2-3 up to the last character in the larger string
4. If you still haven't found a substring match, return 0.

3. ## Re: First string is a substring of another

```public static int substring(String Str1, String Str2)
{
int return = 0
for(int i = 0; i < Str2.length(); i++)
{
if (Str2.charAt(i) == Str1.charAt(0))
for(int j =0; j < Str2.length(); j++)
{
if (Str2.charAt(i) == Str1.charAt(j))

return = 1;
else
return = 0;
}
}
for(int i = 0; i < Str2.length(); i++)
{
if (i = Str2.length())
i = 0
for (int j = 0; j < Str2.length(); j++)

return -1;
}
}
}```

this is the code that I came up with for this problem can anyone see any errors with it

4. ## Re: First string is a substring of another

You're return statements are pre-mature. The program hasn't completely checked that the string is contained str2 is a substring of str1, but you're saying it is. Also, return is a Java keyword. You can't declare variables called return. There are a few other Java syntax errors (missing semi-colons).

5. ## Re: First string is a substring of another

I know I was noticing that about the return statements also

what would be your suggestions to make sure that the program completely checks that str2 is a substring of str1

7. ## Re: First string is a substring of another

```public static int substring(String str1, String str2)
{
for (int i = 0; i+str2.length() < str1.length(); i++)
{
if (str2.equals(str1.substring(i,i+str2.length()))
{
return 1;
}
}
for (int i = str1.length()-str2.length(); i< str1.length(; i++)
{
if (str2.equals(str1.substring(i,str1.length)+str1.substring(0,str2.length-i)))
{
return -1;
}
}
return 0;
}```

Give this a go.

8. ## The Following User Says Thank You to helloworld922 For This Useful Post:

mgutierrez19 (October 20th, 2009)

9. ## Re: First string is a substring of another

tnx hellowolrd... im trying to solve this one.. but im having hard time thinking of how.... hehe

10. ## The Following User Says Thank You to chronoz13 For This Useful Post:

mgutierrez19 (October 20th, 2009)

11. ## Re: First string is a substring of another

Thank you guys very much for all you help

12. ## Re: First string is a substring of another

``` for (int i = str1.length() - str2.length(); i < str1.length(); i++) {

if (str2.equals(str1.substring(i, str1.length() + str1.substring(0, str2.length() - i))))      //cannot find symbol
{
System.out.println("No Its Not!");
}```

Symbol: method subtring(int, java.lang.String)

13. ## Re: First string is a substring of another

you're missing a closing parenthesis

`str2.equals(str1.substring(i, str1.length()[b])[/b] + str1.substring(0, str2.length() - i)))`

ahh hehehe

15. ## Substring

- Checks if the first string is a substring of the second one.
- Returns 0 if it is not a substring / 1 if it is / -1 if it is a “circular substring” (see example).

Example:
- abcde is a substring of blkjabcdelkjsd  return 1
- abcde is not a substring of slfkjsdlskggfhjkh  return 0
- abcde is a “circular substring” of delkjlkjabc  return -1

I had already posted this method before but the last part where it is suppose to check for the circular substring does not seem to be running correctly, can someone look over it and see where i have to make correction on it im not sure if its even right

```public static int subVirus(String Str1, String Str2)
{
int result = 0;
for(int i = 0; i < Str2.length(); i++)
{
if (Str2.charAt(i) == Str1.charAt(0))
for(int j = (i + 1); j < Str2.length(); j++)
{
if (Str2.charAt(j) == Str1.charAt(j))
result = 1;
}
}
for(int i = 0; i < Str2.length(); i++)
{
if (i = Str2.length())
i = 0
for (int j = 0; j < Str2.length(); j++)

return = -1
}

return result;
}```

16. ## Re: First string is a substring of another

subVirus? I merged your two topics and marked the original one as un-solved.

Oops, made a few logical errors in my solution a few posts back.

```	public static int substring(String str1, String str2)
{
if (str2.length() > str1.length())
{
return 0;
}
for (int i = 0; i + str2.length() <= str1.length(); i++)
{
if (str2.equals(str1.substring(i, i + str2.length())))
{
return 1;
}
}
for (int i = str1.length() - str2.length() + 1; i < str1.length(); i++)
{
if (str2.equals(str1.substring(i, str1.length()) + str1.substring(
0, i - str2.length() + str1.length())))
{
return -1;
}
}
return 0;
}```

17. ## The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

chronoz13 (October 22nd, 2009), mgutierrez19 (October 21st, 2009)