aspose file tools*
The moose likes Java in General and the fly likes Getting un-Expected Results From String IndexOf Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting un-Expected Results From String IndexOf Method" Watch "Getting un-Expected Results From String IndexOf Method" New topic
Author

Getting un-Expected Results From String IndexOf Method

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1391
If 'iConn.getItemSpocno().trim()' is a blank string and '(iConn.getSpocFAIExclusionList()' = S070,S110,S120,S130 why does the following still return a 0 value?

Muhammad Safwat
Greenhorn

Joined: Aug 12, 2008
Posts: 12
please post the output of running this code


http://www.thejavacode.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
Because an empty String can be found at the 0 position.

Imagine: if you add nothing to the beginning of that String, it won't change. Therefore it is the same as having an empty String at its very beginning, so you get 0 as the index.
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

Hello Steve,

If you will search blank i.e. "" in any String then it index will always be 0.

Thanks.
 
 
subject: Getting un-Expected Results From String IndexOf Method
 
Similar Threads
indexof
StringTokenizer ??
Extract an Email From a String literal
q from abhilash mock exam on strings.
Why is "String".trim() not equal to " String ".trim()