Starts With (string)

From SwiftAPI

Jump to: navigation, search

Contents

Java

J2SE 1.4.2

java.lang.String : public boolean startsWith(String prefix[, int toffset])
String s = "hello world";
 
String prefix = "hello";
boolean b = s.startsWith(prefix);
// b == true
 
prefix = "world";
b = s.startsWith(prefix);
// b == false

JavaScript

var s = "hello world";
 
var prefix = "hello";
var b = (s.indexOf(prefix) == 0);
// b == true
 
prefix = "world";
b = (s.indexOf(prefix) == 0);
// b == false

Prototype 1.5.1+

prototypejs.org

var s = "hello world";
 
var prefix = "hello";
var b = s.startsWith(prefix);
// b == true
 
prefix = "world";
b = s.startsWith(prefix);
// b == false

Objective-C

developer.apple.com

- (BOOL)hasPrefix:(NSString *)aString
NSString *s = @"hello world";
 
NSString *prefix = @"hello";
BOOL b = [s hasPrefix:prefix];
// b == YES
 
prefix = @"world";
b = [s hasPrefix:prefix];
// b == NO

PHP

$s = "hello world";
 
$prefix = "hello";
$b = (strpos($s, $prefix) === 0);
// $b === true
 
$prefix = "world";
$b = (strpos($s, $prefix) === 0);
// $b === false

Python

docs.python.org

s = "hello world"
 
prefix = "hello"
b = s.startswith(prefix)
# b == True
 
prefix = "world"
b = s.startswith(prefix)
# b == False

Swift

let s = "hello world"

let prefix = "hello"
let b = s.hasPrefix(prefix)
// b == true

let prefix2 = "world"
let b2 = s.hasPrefix(prefix2)
// b2 == false
Personal tools