함수 strpos()

(Strpos에서 넘어옴)
find string
strpos
IndexOf
InStr
FIND
SEARCH

1 C++[ | ]

#include <iostream>
using namespace std;
int main() {
	string s = "Hello World!";
	int pos = s.find("H");
	cout << pos          << endl; // 0
	cout << s.find("ll") << endl; // 2
	cout << s.find("e")  << endl; // 1
	cout << s.find("z")  << endl; // 18446744073709551615
}

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 Go[ | ]

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Index("hello world", "h"))  // 0
	fmt.Println(strings.Index("hello world", "l"))  // 2
	fmt.Println(strings.Index("hello world", "ll")) // 2
	fmt.Println(strings.Index("hello world", "z"))  // -1
}

5 Java[ | ]

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

6 JavaScript[ | ]

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

7 Lisp[ | ]

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

8 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

9 Perl[ | ]

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

10 PHP[ | ]

var_dump( strpos("do you remember?", "d") ); // int(0)
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)

if(strpos("word", "z") === false) {
        echo "not exist";   
}

11 Python[ | ]

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

12 Ruby[ | ]

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

13 Scheme[ | ]

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

14 VB[ | ]

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

15 같이 보기[ | ]