break strings of link in an array xcode - objective-c

Do you know how to break a string in using objective c and fed it into an array ? i am new to c environment and am finding it hard to get this. please help
e.g. x://xxx; x://soe;x://roy
result:
x://wwww
x://soe
x://roy
note: break point ";"

try
NSString *str = x://xxx; x://soe;x://roy;
NSArray *arr = [str componentsSeparatedByString:#";"];

Refer following code this will help you:
NSString *string = #"x://xxx; x://soe;x://roy";
NSArray *listItems = [string componentsSeparatedByString:#";"];
Please do some googling before posting the question.

Related

NSString takes substrings

I have the following NSString in objective C:
[["123"], ["456"], ["adg"]]
and I would like to separate into differents strings like this :
aux = 123;
aux2 = 456;
aux3 = adg;
Please, anyone can help me
Thanks
you can call
NSArray *arrayOfSubStrings = [myString componentsSeparatedByString:#","];
then loop through for each string in the array and remove the square brackets by calling
aux = [stringByReplacingOccurrencesOfString:[arrayOfSubStrings ObjectAtIndex:x] withString:#""]
See NSString Doc for more details
NSString *str = #"[[\"123\"], [\"456\"], [\"adg\"]]";
NSString *new = [[str substringFromIndex:4]substringToIndex:7];
new will be 123
and so on
If you have control over the string format that you're parsing I'd highly recommend looking at JSON and using NSJSONSerialization to parse it. This is more preferable than re-inventing the wheel by defining your own format to store primitive objects.

Padding NSString not working

I have read that to left-pad an NSString all you need to do is this:
NSString *paddedStr = [NSString stringWithFormat:#"%-20.20# %-20.20#",
aString, anotherSting];
But, that does not work !! I don´t know why. I have tried a lot of combinations without success. Examples:
NSString *paddedStr = [NSString stringWithFormat:#"%-20s#", " ", myString];
but that way is ugly and ... ugly. It just append 20 times the char (" ") before the string (myString) and that is not what we need right?
The goal is to have an NSString formatted to present two or more columns of 20 chars each one no matter the length of the string within a row.
Example Goal Output:
Day Hour Name Age
Does anybody know how to do this right?
I'm using ARC and iOS 5.
And actually, the formatted string is going to be written to file using NSFileHandle.
Thanks to all of you folks !!
Edit:
I have noticed that this works:
NSString *str = [NSString stringWithFormat:#"%-10.10s %-10.10s",
[strOne UTF8String], [strTwo UTF8String]];
But... We don't want C-style strings either.
Here is a way to do that :
NSString *paddedStr = [NSString stringWithFormat:#"%#%#",
[#"day" stringByPaddingToLength:20
withString:#" "
startingAtIndex:0],
[#"Hour" stringByPaddingToLength:20
withString:#" "
startingAtIndex:0]];

getting NSArray length/count - Objective C

Well I've looked at similar problems over the site but haven't reached a solution thus far so I must be doing something wrong.
Essentially, I am importing a text file, then splitting each line into an element of an array. Since the text file will be updated etc.. I won't every know the exact amount of lines in the file and therefore how many elements in the array. I know in Java you can do .length() etc.. and supposedly in Objective C you can use 'count' but i'm having no luck returning the length of my array... suggestions?
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"allshows"
ofType:#"txt"];
NSString *fileString = [NSString stringWithContentsOfFile:filePath];
NSArray *lines = [fileString componentsSeparatedByString:#"\n"];
NSUInteger *elements = [lines count];
NSLog(#"Number of Shows : ", elements);
and what is being output is NOTHING. as in "Number of Shows : " - blank, like it didn't even count at all.
Thank you for any help!
You're missing the format string placeholder. It should be:
NSLog(#"Number of shows: %lu", elements);
You need to use a format specifier to print an integer (%d):
NSLog(#"Number of Shows : %d", elements);
Looking at your other post, it seems like you are a Java developer. In Java's System.out, you just append the variables. In Objective-C, I suggest you look at "print format specifiers". Objective-C uses the same format.

Split an NSString to access one particular piece

I have a string like this: #"10/04/2011" and I want to save only the "10" in another string. How can I do that?
NSArray* foo = [#"10/04/2011" componentsSeparatedByString: #"/"];
NSString* firstBit = [foo objectAtIndex: 0];
Update 7/3/2018:
Now that the question has acquired a Swift tag, I should add the Swift way of doing this. It's pretty much as simple:
let substrings = "10/04/2011".split(separator: "/")
let firstBit = substrings[0]
Although note that it gives you an array of Substring. If you need to convert these back to ordinary strings, use map
let strings = "10/04/2011".split(separator: "/").map{ String($0) }
let firstBit = strings[0]
or
let firstBit = String(substrings[0])
Either of these 2:
NSString *subString = [dateString subStringWithRange:NSMakeRange(0,2)];
NSString *subString = [[dateString componentsSeparatedByString:#"/"] objectAtIndex:0];
Though keep in mind that sometimes a date string is not formatted properly and a day ( or a month for that matter ) is shown as 8, rather than 08 so the first one might be the worst of the 2 solutions.
The latter should be put into a separate array so you can actually check for the length of the thing returned, so you do not get any exceptions thrown in the case of a corrupt or invalid date string from whatever source you have.
Its working fine
NSString *dateString = #"10/10/2010";//Date
NSArray* dateArray = [dateString componentsSeparatedByString: #"/"];
NSString* dayString = [dateArray objectAtIndex: 0];
Objective-c:
NSString *day = [#"10/04/2011" componentsSeparatedByString:#"/"][0];
Swift:
var day: String = "10/04/2011".componentsSeparatedByString("/")[0]
Use [myString componentsSeparatedByString:#"/"]
I have formatted the nice solution provided by JeremyP above into a more generic reusable function below:
///Return an ARRAY containing the exploded chunk of strings
+(NSArray*)explodeString:(NSString*)stringToBeExploded WithDelimiter:(NSString*)delimiter
{
return [stringToBeExploded componentsSeparatedByString: delimiter];
}
Swift 3.0 version
let arr = yourString.components(separatedBy: "/")
let month = arr[0]

stringByAppendingFormat not working

I have an NSString and fail to apply the following statement:
NSString *myString = #"some text";
[myString stringByAppendingFormat:#"some text = %d", 3];
no log or error, the string just doesn't get changed. I already tried with NSString (as documented) and NSMutableString.
any clues most welcome.
I would suggest correcting to (documentation):
NSString *myString = #"some text";
myString = [myString stringByAppendingFormat:#" = %d", 3];
From the docs:
Returns a string made by appending to the receiver a string constructed from a given format string and the following arguments.
It's working, you're just ignoring the return value, which is the string with the appended format. (See the docs.) You can't modify an NSString — to modify an NSMutableString, use -appendFormat: instead.
Of course, in your toy example, you could shorten it to this:
NSString *myString = [NSString stringWithFormat:#"some text = %d", 3];
However, it's likely that you need to append a format string to an existing string created elsewhere. In that case, and particularly if you're appending multiple parts, it's good to think about and balance the pros and cons of using a mutable string or several immutable, autoreleased strings.
Creating strings with #"" always results in immutable strings. If you want to create a new NSMutableString do it as following.
NSMutableString *myString = [NSMutableString stringWithString:#"some text"];
[myString appendFormat:#"some text = %d", 3];
I had a similar warning message while appending a localized string. This is how I resolved it
NSString *msgBody = [msgBody stringByAppendingFormat:#"%#",NSLocalizedString(#"LOCALSTRINGMSG",#"Message Body")];