I have a NSMutableString variable that i append from aString, like this:
NSString *aString = [NSString stringWithFormat:#"{\"MyCode\":%#,\"TotalAmount\":%#,\"PaymentType\":\"%#\",\"BankName\":\"%#\",\"BankAccountNo\":\"%#\",\"Amount\":\"%#\",\"FileName\":\"%#\"}",aCode,total,type,labelBank.txt,labelAcc.txt,aTrasfer,imageName];
[teststring appendString:asstring2];
[teststring appendString:#","];
in this code i sucess to append the string in order they append. But know i want to append a new string in the first position, just like in array object at index 0.Can NSMutableString do this?
Hope my question is clear..thanks
You said it, with an index. Same way as you do with an array.
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)anIndex
Your code:
[testString appendString:aString];
[testString insertString:#"," atIndex:0]; /* prepend the comma
Check out the Documentation
Related
I have an NSArray, where each object contains a specific class called Card. Card has a description method. I want to join all objects in the array using the output of the description method, separated by spaces. Is there a simple to do this, without manually iterating the NSArray and manipulating NSString?
Something akin to the following made-up code?
NSArray *myArray = getCards(); // fetches 10 items or more
NSString *myString = [myArray joinUsingDescriptionMethodSeparatedBy:#" "];
or
NSString *myString = [NSString stringFromArrayDescriptionMethods:myArray separatedBy:#" "];
Naturally ,I could implement this myself but I suspect there could be something already present that does this.
I don't think that there is such a method. You can also implement it in a Category for NSString.
Sorry, I found this:
NSString * result = [[array valueForKey:#"description"] componentsJoinedByString:#""];
From the documentation:
Constructs and returns an NSString object that is the result of
interposing a given separator between the elements of the array.
- (NSString *)componentsJoinedByString:(NSString *)separator
Do this for description method of each NSArray item:
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
[result appendString:[NSString stringWithFormat:#" %#"[obj description]]];
}
NSLog(#"The concatenated string is %#", result);
How to change value of mutable string ? Here is what I do
NSString *str = #"This is string";
NSMutableString *str = [NSMutableString stringWithFormat:#"%#", str];
str = #"New string" -> wrong incompatible pointer types assigning to NSMutableString from NSString
You only need to use NSMutableString if you want to change parts of the string in place (append, insert etc.), often for performance reasons.
If you want to assign new values to the string variable, you're fine with a good old NSString as your last line simple assigns a complete new string object to str:
You can use setString to replace the whole string:
NSString *str = #"This is string";
NSMutableString *mutableStr = [NSMutableString stringWithFormat:#"%#", str];
...
[mutableStr setString:#"a different non mutable string"];
As indicated in another answer, a non-mutable NSString may be enough for your purposes.
This is how you should initialize a NSMutableString:
NSMutableString *string = [[NSMutableString alloc]init];
You could use any other way specified in the docs. The way you are doing it, you are not creating any instance of the NSMutableString class. Then, if you want to add some string to it:
[string appendString:#"content"];
I have a set of NSString representing the names of the files in a directory. These names are structured this way:
XXXXXXXXX_YYYY_AAAA.ext
All the sections separated by "_" are of variable length and I would only have the first.
How can I separate the first part from the other?
Find the position of the '_' character, then get a substring 0 through that position. Note that substringToIndex: does not include the character at the index position.
NSRange r = [myString rangeOfString:#"_"];
NSString *res = [myString substringToIndex:r.location];
Take a look at the NSString method componentsSeparatedByString:. That will tokenize a string and return you an array. Something like this:
NSArray *array = [#"XXXXXXXXX_YYYY_AAAA.ext" componentsSeparatedByString:#"_"];
NSString *firstToken = [array objectAtIndex:0];
NSArray *array = [yourString componentsSeparatedByString:#"_"];
NSString *Xs = [array objectAtIndex:0];
Try componentsSeparatedByString: under the heading Dividing Strings.
NSString Docs
i want to delete numbers individually by this button from my label
NSMutableString *str=(NSMutableString *)label.text;
str=[str replaceCharacterInRange:NSMakeRange([str length]-1,1) withString:#""];
error....."Void value not ignored as it ought to be"
You can't just cast an NSString as an NSMutableString and expect it to be mutable. You need to create a mutable string before you alter it.
NSMutableString *mutableString = [label.text mutableCopy];
[mutableString replaceCharactersInRange:NSMakeRange([mutableString length] - 1, 1) withString:#""];
replaceCharacterInRange:withString: returns void since it is a mutable operation that modifies the string.
To fix your problem, the first thing you need to know is that you can not make a string mutable just by casting it as NSMutableString you need to use mutableCopy.
NSMutableString *str= [label.text mutableCopy];
//Now the next thing do not assign str
[str replaceCharacterInRange:NSMakeRange([str length]-1,1) withString:#""];
...
//And finally when you are done if you are not using ARC
///then you need to release the string since you called `mutableCopy`.
[str release];
use deleteCharactersInRange:
[str deleteCharacterInRange:NSMakeRange([str length]-1,1) ])]
How to print a array element at particular index in Objective-C? My code looks like this:
NSString *String=[NSString StringWithContentsOFFile:#"/User/Home/myFile.doc"];
NSString *separator = #"\n";
NSArray *array = [String componetntsSeparatedByString:separator];
NSLog(#"%#",array);
I'm able to print the full contents of an array at once, but I want to assign the element at each index into a string, like...
str1=array[0];
str2=array[1];
str3=array[0];...this continues
How do I do this?
You want the objectAtIndex: method. Example:
NSString *str1 = [array objectAtIndex:0];
NSString *str2 = [array objectAtIndex:1];
NSString *str3 = [array objectAtIndex:2];
From the documentation:
objectAtIndex:
Returns the object located at index.
- (id)objectAtIndex:(NSUInteger)index
Parameters
index
An index within the bounds of the receiver.
Return Value
The object located at index.
Discussion
If index is beyond the end of the array (that is, if index is greater than or equal to the value returned by count), an NSRangeException is raised.
if this is only for debugging, you could try using po <> in the gdb.
As of clang version 3.3, you can use the [index] notation, so
NSString *str1 = array[0];
would work now. See here for details.