This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes String is in  String array or not  ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String is in  String array or not  ?" Watch "String is in  String array or not  ?" New topic
Author

String is in String array or not ?

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
How to find particular string value in the String array.

String[] names = new String[];
names[0] = joe;
names[1] = james;
names[2] = marry;

String person = "james";

How can I find whether james is in names String array or not ? Any API methods supports ?


Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
There are no standard methods to search for a particular element in an unsorted array, but there are methods to search a sorted array. Alternatively you could convert the array to a List<String> using java.util.Arrays.asList() and use the List.contains() method. Or you could write your own linear search method.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Michal Winther
Greenhorn

Joined: Apr 03, 2009
Posts: 1
kri shan wrote:How to find particular string value in the String array.

String[] names = new String[];
names[0] = joe;
names[1] = james;
names[2] = marry;

String person = "james";

How can I find whether james is in names String array or not ? Any API methods supports ?




Pretty simple:

Use the Strings own compare function: equals() or equalsIgnoreCase().
Both return boolean.

Example:

for(int i=0; i<names.length; i++){
if(names[i].equalsIgnoreCase(person)) return true;
}>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String is in String array or not ?
 
Similar Threads
Array problem
How to iterate through c:forEach tag
using prepare statements
find methods
sort the String values in String array?