함수 indexOf()

(IndexOf에서 넘어옴)
  다른 뜻에 대해서는 리눅스 find 문서를 참조하십시오.
find string
strpos
IndexOf
InStr
FIND
SEARCH
  • Find position of first occurrence of a string

1 Bash[ | ]

STR="Hello World!"
echo $STR | awk '{print index($0,"ll")}' # 3
echo $STR | awk '{print index($0,"ld")}' # 10
echo $STR | awk '{print index($0,"H")}'  # 1
echo $STR | awk '{print index($0,"z")}'  # 0

2 C#[ | ]

string str = "Hello World!";
int pos = str.IndexOf("ll");	// 2
"Hello mate".IndexOf("e");	// 1
"Hello mate".IndexOf("e", 4);	// 9
"word".IndexOf("z");		// -1

3 Excel[ | ]

  • ※ 1-based index
=FIND("h", "hello world")
=SEARCH("h", "hello world")
// 1
=FIND("ll", "hello world")
=SEARCH("ll", "hello world")
// 3
=FIND("z", "hello world")
// #VALUE!

4 Java[ | ]

public class MyClass {
    public static void main(String [] args) {
        String str = "Hello World!";
        int pos = str.indexOf("ll");
        System.out.println(pos); // 2
    }
}
public class MyClass {
    public static void main(String [] args) {
        System.out.println("Hello mate".indexOf("e")); // 1
        System.out.println("Hello mate".indexOf("e", 4)); // 9
        System.out.println("word".indexOf("z")); // -1
    }
}

5 JavaScript[ | ]

console.log("Hello mate".indexOf("e")  ); // 1
console.log("Hello mate".indexOf("e",4)); // 9
console.log("Hello mate".indexOf("z",4)); // -1

6 Lisp[ | ]

(search "e" "Hello mate")             ;  returns 1
(search "z" "word")                   ;  returns NIL

7 Objective-C[ | ]

[@"do you remember?" rangeOfString:@"you"]; // returns NSMakeRange(3, 3)
[@"do you remember?" rangeOfString:@"e" options:0 range:NSMakeRange(11, 5)]; // returns NSMakeRange(13, 1)
[@"do you remember?" rangeOfString:@"z"]; // returns NSMakeRange(NSNotFound, 0)
#define STRPOS(str, x) [Util strpos:str sub:x]
+(int) strpos:(NSString*)str sub:(NSString*)sub {
    int pos = [str rangeOfString:sub].location;
    return (pos == NSNotFound)? -1 : pos;
}
NSLog(@"%d", STRPOS(@"do you remember?", @"you")); // 3
NSLog(@"%d", STRPOS(@"do you remember?", @"e")); // 8
NSLog(@"%d", STRPOS(@"do you remember?", @"z")); // -1

8 Perl[ | ]

print index("do you remember?", "you");   # 3
print index("do you remember?", "e", 11); # 13
print index("word", "z");                 # -1

9 PHP[ | ]

var_dump( strpos("do you remember?", "you") );   # int(3)
var_dump( strpos("do you remember?", "e", 11) ); # int(13)
var_dump( strpos("word", "z") ); # bool(false)

10 Python[ | ]

print( "do you remember?".find("you") )   # 3
print( "do you remember?".find("e", 11) ) # 13
print( "word".find("z") )  # -1
print( "word".index("z") ) # ValueError

11 Ruby[ | ]

puts "do you remember?".index("you")   # 3
puts "do you remember?".index("e", 11) # 13
puts "word".index("z")         # 
puts "word".index("z").inspect # nil

12 Scheme[ | ]

(use-modules (srfi srfi-13))
(string-contains "Hello mate" "e")    ;  returns 1
(string-contains "word" "z")          ;  returns #f

13 VB[ | ]

InStr("Hello mate", "e")              '  returns 2
InStr(5, "Hello mate", "e")           '  returns 10
InStr("word", "z")                    '  returns 0

14 같이 보기[ | ]