String Index Of

From SwiftAPI

Jump to: navigation, search

Contents

C

msdn

#include <string.h>
 
char* haystack = "Hello World";
char* p;
 
p = strstr(haystack, "Wo");
// p == haystack + 6 (p points to the start of the substring)
 
p = strstr(haystack, "Z");
// p == NULL

C++

#include <string>
using namespace std;
 
string haystack = "Hello World";
int i;
 
i = haystack.find("Wo");
// i == 6
 
i = haystack.find("Z");
// i == string::npos

C#

msdn

string haystack = "Hello World";
int i;
 
i = haystack.IndexOf("Wo");
// i == 6
 
i = haystack.IndexOf("Z");
// i == -1

Java

J2SE 1.4.2

java.lang.String : public int indexOf(String str[, int fromIndex])
String haystack = "Hello World";
 
int i = haystack.indexOf("Wo");
// i == 6
 
i = haystack.indexOf("Z");
// i == -1

JavaScript

developer.mozilla.org

var haystack = "Hello World";
 
var i = haystack.indexOf("Wo");
// i == 6
 
i = haystack.indexOf("Z");
// i == -1

Objective-C

developer.apple.com

- (NSRange)rangeOfString:(NSString *)aString
NSString *haystack = @"Hello World";
 
int i = [haystack rangeOfString:@"Wo"].location;
// i == 6
 
i = [haystack rangeOfString:@"Z"].location;
// i == -1

Perl

perldoc

index STR,SUBSTR,POSITION
index STR,SUBSTR
$haystack = "Hello World";
 
$i = index($haystack, "Wo");
# $i == 6
 
$i = index($haystack, "Z");
# $i == -1

PHP

php.net

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
$haystack = "Hello World";
 
$i = strpos($haystack, "Wo");
// $i === 6
 
$i = strpos($haystack, "Z");
// $i === false

Python

docs.python.org

haystack = "Hello World"
 
i = haystack.find("Wo")
# i == 6
 
i = haystack.find("Z")
# i == -1

Ruby

Ruby-Doc.org

index(substring [, offset]) → fixnum or nil
index(regexp [, offset]) → fixnum or nil
haystack = "Hello World"
 
i = haystack.index("Wo")
# i == 6
 
i = haystack.index("Z")
# i == nil
Personal tools