Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Comparing two strings

Reply
Thread Tools

Comparing two strings

 
 
HS1
Guest
Posts: n/a
 
      11-28-2004
Hello all

I want to compare two string to see whether they have a matching pattern or
not. For example:
"we are students" will match with "are"
I tried functions string.matches or string.regionMatches but they did not
work

Could you please help
Thank you
SH1


 
Reply With Quote
 
 
 
 
Rob van der Leek
Guest
Posts: n/a
 
      11-28-2004
In article <1101678692.849793@ftpsrv1>, HS1 wrote:
> Hello all
>
> I want to compare two string to see whether they have a matching pattern or
> not. For example:
> "we are students" will match with "are"
> I tried functions string.matches or string.regionMatches but they did not
> work


What you are looking for is a substring match, you can accomplish this
with the indexOf(...) method, e.g.:

public class T {
public static void main(String args[])
{
String s1 = "we are students";
String s2 = "are";
int startIndex = s1.indexOf(s2);
if (startIndex > 0) {
System.out.println("Found substring at: " + startIndex);
}
else {
System.out.println("Found no match");
}
}
}

Regards,
--
Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
 
Reply With Quote
 
 
 
 
MaSTeR
Guest
Posts: n/a
 
      11-29-2004
> public class T {
> public static void main(String args[])
> {
> String s1 = "we are students";
> String s2 = "are";
> int startIndex = s1.indexOf(s2);
> if (startIndex > 0) {
> System.out.println("Found substring at: " + startIndex);
> }
> else {
> System.out.println("Found no match");
> }
> }
> }
>


Actually
if (startIndex != -1) {
System.out.println("Found substring at: " + startIndex);
}
else {
System.out.println("Found no match");
}
}
}

Otherwise wouldn't find a match for
String s1 = "we are students";
String s2 = "we";


 
Reply With Quote
 
Rob van der Leek
Guest
Posts: n/a
 
      11-29-2004
In article <(E-Mail Removed)>, MaSTeR wrote:
>> public class T {
>> public static void main(String args[])
>> {
>> String s1 = "we are students";
>> String s2 = "are";
>> int startIndex = s1.indexOf(s2);
>> if (startIndex > 0) {
>> System.out.println("Found substring at: " + startIndex);
>> }
>> else {
>> System.out.println("Found no match");
>> }
>> }
>> }
>>

>
> Actually
> if (startIndex != -1) {
> System.out.println("Found substring at: " + startIndex);
> }
> else {
> System.out.println("Found no match");
> }
> }
> }
>
> Otherwise wouldn't find a match for
> String s1 = "we are students";
> String s2 = "we";


Oops, you're right, actually I always use >= .

Thanks!

--
Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing two strings with arrays and pointers agent349 C++ 5 03-10-2009 04:23 AM
Two identical Strings stored in two different object Neroku Java 12 02-12-2007 03:32 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
C++ function for comparing two date strings... ad C++ 1 12-13-2004 10:24 PM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments