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 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 Murach's Java Servlets and JSP this week in the Servlets 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: 1393
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: 37950
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting un-Expected Results From String IndexOf Method
 
Similar Threads
Why is "String".trim() not equal to " String ".trim()
Extract an Email From a String literal
indexof
StringTokenizer ??
q from abhilash mock exam on strings.