Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 14th, 2009, 04:24 AM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default String comparision

Hi,
I have two strings with values say like

String s1 ="hello,test,from,one,party";
String s2 = "into,bytes,hello,to";

i need to compare s1 values with s2 values if exists i need to return the matched value.

can any one help how to proceed.

regards
raj
 
Old April 14th, 2009, 04:19 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Split the two strings into two arrays and then use a nested loop that compares the array elements. Yes, there is a method on the String class that will do the split for you.
 
Old April 21st, 2009, 08:37 AM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Code:
 
        Set<String> set = new HashSet<String>();
        set.addAll(Arrays.asList(s1.split(",")));
        set.retainAll(Arrays.asList(s2.split(",")));
 
Old April 21st, 2009, 08:41 AM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Quote:
Originally Posted by Old Pedant View Post
... and then use a nested loop that compares the array elements...
Not needed.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how t do comparision of elements in xsl suji XSLT 2 February 28th, 2008 12:27 PM
Date comparision in asp.net dharmeshtandel ASP.NET 2.0 Basics 4 December 22nd, 2007 02:20 PM
Code comparision ayazhoda Access VBA 2 June 27th, 2007 05:35 AM
comparision of two word documents anuradhad VB How-To 1 July 24th, 2004 04:10 PM
comparision of two documents anuradhad Access VBA 0 July 22nd, 2004 12:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.