Difference between revisions of "Documentation/How Tos/Calc: FIND function"
From Apache OpenOffice Wiki
< Documentation | How Tos
m |
(Added example to cover case where substring is not present.) |
||
Line 20: | Line 20: | ||
<tt>'''FIND("cho"; "choochoo"; 2)'''</tt> | <tt>'''FIND("cho"; "choochoo"; 2)'''</tt> | ||
: returns <tt>'''5'''</tt>. | : returns <tt>'''5'''</tt>. | ||
+ | |||
+ | <tt>'''FIND("xyz","abcdef",1)'''</tt> | ||
+ | : returns <tt>'''#VALUE!'''</tt>. | ||
+ | : NOTE: This is an error condition, which must be 'handled' if used as the argument to another function. | ||
+ | : <tt>'''IF( FIND("xyz","abcdef",1) , "Substring Present", "ERR: Missing Substring" )'''</tt> | ||
+ | :: returns <tt>'''#VALUE!'''</tt> which is not very useful, therefore we could use either <tt>'''ISERROR()'''</tt> or <tt>'''ISNUMBER()'''</tt> for example: | ||
+ | :: <tt>'''IF( ISERROR( FIND("xyz","abcdef",1) ) , "ERR: Missing Substring", "Substring Present" )'''</tt>. | ||
+ | ::: returns <tt>'''"ERR: Missing Substring"'''</tt> (... allowing the IF() to function, and not propagating the error from the FIND() function. | ||
+ | :: <tt>'''IF( ISNUMBER( FIND("xyz","abcdef",1) ) , "Substring Present", "ERR: Missing Substring" )'''</tt>. | ||
+ | ::: returns <tt>'''"ERR: Missing Substring"'''</tt> (... allowing the IF() to function, and not propagating the error from the FIND() function. | ||
+ | :: NOTE: In practice, it may be more maintainable to use ISNUMBER() to avoid negative logic, and it is more indicitive of the evaluation desired: if the substring has a position, then ISNUMBER() is TRUE, else ISNUMBER() is FALSE. | ||
{{Documentation/SeeAlso| | {{Documentation/SeeAlso| |
Revision as of 01:59, 18 February 2011
FIND
Returns the position of a string of text within another string.
Syntax:
FIND(findtext; texttosearch; startposition)
- returns the character position of the first occurrence of findtext within texttosearch.
- startposition (optional) is the position from which the search starts.
- The search is case-sensitive.
- A failed search gives the #VALUE! error.
Example:
FIND("yo"; "Yoyo")
- returns 3.
FIND("cho"; "choochoo"; 2)
- returns 5.
FIND("xyz","abcdef",1)
- returns #VALUE!.
- NOTE: This is an error condition, which must be 'handled' if used as the argument to another function.
- IF( FIND("xyz","abcdef",1) , "Substring Present", "ERR: Missing Substring" )
- returns #VALUE! which is not very useful, therefore we could use either ISERROR() or ISNUMBER() for example:
- IF( ISERROR( FIND("xyz","abcdef",1) ) , "ERR: Missing Substring", "Substring Present" ).
- returns "ERR: Missing Substring" (... allowing the IF() to function, and not propagating the error from the FIND() function.
- IF( ISNUMBER( FIND("xyz","abcdef",1) ) , "Substring Present", "ERR: Missing Substring" ).
- returns "ERR: Missing Substring" (... allowing the IF() to function, and not propagating the error from the FIND() function.
- NOTE: In practice, it may be more maintainable to use ISNUMBER() to avoid negative logic, and it is more indicitive of the evaluation desired: if the substring has a position, then ISNUMBER() is TRUE, else ISNUMBER() is FALSE.