Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-06-2007, 03:17 AM
Member
 
Join Date: Nov 2007
Posts: 7
nalinda is on a distinguished road
searching
given a sequence of letters, and an array that has a random sequence of letters in each element, how do i find if any of the elements match the given sequence, and which element number it is at?
i.e:given "abcdefg" , String [] array = {bfg, fgj, cde, efg, ght} , how do i say that cde and efg are inside that sequence?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-06-2007, 03:49 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
You can always use a regex. If you dont know what a regex is, i strongly suggest you read Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
Code:
// all the patterns that are going to have to be matched String [] array = {"bfg", "fgj", "cde", "efg", "ght"}; Matcher m; // scans trough each pattern for(int i = 0; i < array.length; i++) { // creates a new pattern for each element in the array Pattern p = Pattern.compile(array[i]); // attepts to match the created pattern to the specified string m = p.matcher("abcdefg"); // if the pattern matches: print if (m.find()) System.out.println(array[i] + " is contained"); }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-06-2007, 03:53 AM
Member
 
Join Date: Nov 2007
Posts: 7
nalinda is on a distinguished road
i see what u mean...but how would i do it using indexOf?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-06-2007, 03:56 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Here is a simpler way, although less powerful, it might be better suited for whatever you are doing.
Code:
String [] array = {"bfg", "fgj", "cde", "efg", "ght"}; String toTest = "abcdefg"; for(int i = 0; i < array.length; i++) { if (toTest.contains(array[i])) System.out.println(array[i] + " is contained"); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching an arraylist adelgado0723 New To Java 1 04-15-2008 02:09 PM
Sorting, Searching, and Inserting into a sorted array Java Tip java.lang 0 04-14-2008 09:39 PM
recursively searching through arraylists newtojava7 New To Java 1 03-17-2008 03:36 AM
searching within a JList newtojava7 New To Java 1 03-10-2008 01:12 AM
Searching XML file using DOM simon XML 3 08-01-2007 05:38 PM


All times are GMT +3. The time now is 04:23 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org