Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 828 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I check if a string contains another string in Objective-C?

#11
Please use this code

<!-- language: lang-c -->

NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound)
{
NSLog(@"string does not contain bla");
}
else
{
NSLog(@"string contains bla!");
}
Reply

#12
try this,

<!-- language: lang-c -->

NSString *string = @"test Data";
if ([[string lowercaseString] rangeOfString:@"data"].location == NSNotFound)
{
NSLog(@"string does not contain Data");
}
else
{
NSLog(@"string contains data!");
}
Reply

#13
Best solution. As simple as this! If you want to find a word or
part of the string. You can use this code. In this example we are going to check if the value of word contains "acter".

NSString *word =@"find a word or character here";
if ([word containsString:@"acter"]){
NSLog(@"It contains acter");
} else {
NSLog (@"It does not contain acter");
}

Reply

#14
Since this seems to be a high-ranking result in Google, I want to add this:

iOS 8 and OS X 10.10 add the [`containsString:`][1] method to `NSString`. An updated version of Dave DeLong's example for those systems:

NSString *string = @"hello bla bla";
if ([string containsString:@"bla"]) {
NSLog(@"string contains bla!");
} else {
NSLog(@"string does not contain bla");
}

[1]:

[To see links please register here]

Reply

#15
In Swift 4:

let a = "Hello, how are you?"
a.contains("Hello") //will return true
Reply

#16
With **iOS 8** and **Swift**, we can use `localizedCaseInsensitiveContainsString` method

let string: NSString = "Café"
let substring: NSString = "É"

string.localizedCaseInsensitiveContainsString(substring) // true
Reply

#17
Use the option NSCaseInsensitiveSearch with rangeOfString:options:

NSString *me = @"toBe" ;
NSString *target = @"abcdetobe" ;
NSRange range = [target rangeOfString: me options: NSCaseInsensitiveSearch];
NSLog(@"found: %@", (range.location != NSNotFound) ? @"Yes" : @"No");
if (range.location != NSNotFound) {
// your code
}

Output result is found:Yes

The options can be "or'ed" together and include:

NSCaseInsensitiveSearch
NSLiteralSearch
NSBackwardsSearch and more
Reply

#18
First string contain or not second string,

NSString *first = @"Banana";
NSString *second = @"BananaMilk";
NSRange range = [first rangeOfString:second options:NSCaseInsensitiveSearch];

if (range.length > 0) {
NSLog(@"Detected");
}
else {
NSLog(@"Not detected");
}

Reply

#19
Try this:

**Swift 4.1 , 4.2:**

let stringData = "Black board"

//swift quick way and case sensitive
if stringData.contains("bla") {
print("data contains string");
}

//case sensitive
if stringData.range(of: "bla",options: .caseInsensitive) != nil {
print("data contains string");
}else {
print("data does not contains string");
}

**For Objective-C:**

NSString *stringData = @"Black board";

//Quick way and case sensitive
if ([stringData containsString:@"bla"]) {
NSLog(@"data contains string");
}

//Case Insensitive
if ([stringData rangeOfString:@"bla" options:NSCaseInsensitiveSearch].location != NSNotFound) {
NSLog(@"data contains string");
}else {
NSLog(@"data does not contain string");
}
Reply

#20
**Swift 4 And Above**

let str = "Hello iam midhun"

if str.contains("iam") {
//contains substring
}
else {
//doesn't contain substring
}

**Objective-C**

NSString *stringData = @"Hello iam midhun";

if ([stringData containsString:@"iam"]) {
//contains substring
}
else {
//doesn't contain substring
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through