aspose file tools*
The moose likes Beginning Java and the fly likes find value in string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "find value in string" Watch "find value in string" New topic
Author

find value in string

Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

String se[]={"1","2" ,"3","4"};

may i know how to find if String se[] arry has vaule 3 in it?
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 118
Well, if you don't want to use foreach, for loop you can convert this array to list and then use ".contains" method to detect the presence of desired string.


Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
String[] se, surely, not String se[].
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 118
String[] se, surely, not String se[].


Both are valid. String[] se is simple to understand.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

actually, there is no need convert to list and then search. another way is:

1. sort the array by using java.util.Arrays//this step is must since you are going for binary search

2.then use binarySearch of java.util.Arrays to find out( the index) the element(3) of the array

3.the binarySearch returns >=0 if and only if the key is found

hth
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Siddhesh Deodhar wrote:Both are valid. String[] se is simple to understand.


correct
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Siddhesh Deodhar wrote:Both are valid. String[] se is simple to understand.
String[] se is the recommended format, because the type of se is an array of Strings.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: find value in string