NSString select part go a string objective-c - iphone

I have problem with string. The string shows: ~00000000:termometr2: +26.9 st.C and I want to use only this part: +26.9 st.C in my textfield.text.
Thanks

NSString *fullStr = #"00000000:termometr2: +26.9 st.C";
NSArray *parts = [fullStr componentsSeparatedByString:#" "];
textField.text =[NSString stringWithFormat:#"%#",[parts objectAtIndex:1]];

it might help you:
NSArray *_array = [yourString componentsSeparatedByString:#":"];
[myTextField setText:[_array lastObject]]; // or any other component you want

Just do it this way, using the method stringByReplacingOccurrencesOfString: withString::
NSString *originalString = #"~00000000:termometr2: +26.9 st.C";
NSString *filteredString = [originalString stringByReplacingOccurrencesOfString:#"~00000000:termometr2: " withString:#""];

Related

how to give space between two string while concatnating

I am concatenating two string using following way I want space between Added Content To My Learning And the second String any idea how to give space thanks
NSString *firstString =#"Added Contnet To My Learning";
NSString *secondString = appDelegate.activity_Description;
appDelegate.activity_Description = [firstString stringByAppendingString:secondString];
You can do it like this.
NSString *firstString =#"Added Contnet To My Learning";
NSString *secondString = appDelegate.activity_Description;
appDelegate.activity_Description = [NSString stringWithFormat:#"%# %#", firstString, secondString];
Did you try?
NSString *firstString =#"Added Contnet To My Learning ";
// a space after the first string
Here is all way you can do with string
//1st Way
NSString *finalString = [NSString stringWithFormat:#"%# %#",firstString,secondString];
//2nd Way
NSString *finalString = [firstString stringByAppendingFormat:#" %#",secondString];
//3rd way
NSArray *ary= [NSArray arrayWithObjects:firstString,secondString, nil];
NSString *finalString= [ary componentsJoinedByString:#" "];
Follow this how to concatenate two strings in iphone

Convert String into special - splitting an NSString

I have a string like: "mocktail, wine, beer"
How can I convert this into: "mocktail", "wine", "beer"?
the following gives you the desired result:
NSString *_inputString = #"\"mocktail, wine, beer\"";
NSLog(#"input string : %#", _inputString);
NSLog(#"output string : %#", [_inputString stringByReplacingOccurrencesOfString:#", " withString:#"\", \""]);
the result is:
input string : "mocktail, wine, beer"
output string : "mocktail", "wine", "beer"
You need to use:
NSArray * components = [myString componentsSeparatedByString: #", "];
NSString *string = #"mocktail, wine, beer";
//remove whitespaces
NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//get array of string
NSArray *array = [trimmedString componentsSeparatedByString:#","];
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (NSString *trimmedString in array) {
NSString *newString = [NSMutableString stringWithFormat:#"'%#'", trimmedString];
[newArray addObject:newString];
}
//merge new strings
NSString *finalString = [NSString stringWithFormat:#"%#", [newArray objectAtIndex:0]];
for (NSInteger i = 1; i < [newArray count]; i++) {
finalString = [NSString stringWithFormat:#"%#, %#", finalString, [newArray objectAtIndex:i]];
}
Without knowing spesifically about iOS or objective-c, I assume you could use a split function.
In almost any higher level programming language there is such a function.
Try:
Objective-C split
This gets you an array of Strings. You can then practically do with those what you want to do, e.g. surrounding them with single quotes and appending them back together. :D

Adding Onto NSString

I have an NSString that I would like to add extra characters to. In my mind I thought it would be something simple like this:
NSString *answerString = [NSString stringWithFormat:#"%f", finalVolume] + #" Cubic Feet";
But that did not work. Does anyone know what I might be missing here? Thanks!
NSString is immutable, so you cannot just add to it. Instead, you either compose your string like this:
NSString *answerString = [NSString stringWithFormat:#"%f Cubic Feet", finalVolume];
or
NSString *unit = #"Cubic Feet";
NSString *answerString = [NSString stringWithFormat:#"%f %#", finalVolume, unit];
or create a mutable one:
NSMutableString *answerString = [NSMutableString stringWithFormat:#"%f ", finalVolume];
[answerString appendString:#"Cubic Feet"];
Use NSMutableString
You can append anything you like...
I am pretty sure you can do the following:
NSString *answerString = [NSString stringWithFormat:#"%f Cubic Feet", finalVolume];
or if the part being appended needs to be variable you can do the following:
NSString *answerString = [NSString stringWithFormat:#"%f %#", finalVolume, myVariable];
[[NSString stringWithFormat:#"%f", finalVolume] stringByAppendingString:#" Cubic Feet"];
Simply use
NSString *answerString = [NSString stringWithFormat:#"%f %#", finalVolume,#"Cubic Feet"];
NSMutableString *str = [[NSMutableString alloc] init];
[str appendString:#"s1"];
[str appendString:#"s2"];
[NSString stringwithformat:#"%f %#", final value, #"cubic feet"];

Is there any possibility to use more than one NSCharacterSet Object for the same NSString?

Consider this code:
NSString *aString = #"\tThis is a sample string";
NSString *trimmedString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#"The trimmed string: %#",trimmedString);
trimmedString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"string"]];
NSLog(#"The trimmed string: %#",trimmedString);
Here if I use characterSetWithCharactersInString: on the same NSString object trimmedString, my previous whitespace trimming effect gets removed..
My question is,
Is there any possibility to use more than one NSCharacterSet object to the same NSString???
or Suggest me some other way to do this please, but the NSString object should be one and the same..
The problem is not because of character sets. Its because you are using aString while trimming the string second time. You should use trimmedString instead. Your code should look like,
trimmedString = [trimmedString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"string"]];
What about this:
NSString *aString = #"\tThis is a sample string";
NSMutableCharacterSet *customSet = [[NSMutableCharacterSet alloc] init];
[customSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
[customSet addCharactersInString:#"string"];
NSString *trimmedString = [aString stringByTrimmingCharactersInSet:customSet];
[customSet release];

how to remove particular words from strings?

I have an NSString *str, having value #"I like Programming and gaming."
I have to remove "I" "like" & "and" from my string so it should look like as "Programming gaming"
How can I do this, any Idea?
NSString *newString = #"I like Programming and gaming.";
NSString *newString1 = [newString stringByReplacingOccurrencesOfString:#"I" withString:#""];
NSString *newString12 = [newString1 stringByReplacingOccurrencesOfString:#"like" withString:#""];
NSString *final = [newString12 stringByReplacingOccurrencesOfString:#"and" withString:#""];
Assigned to wrong string variable edited now it is fine
NSLog(#"%#",final);
output : Programming gaming
NSString * newString = [#"I like Programming and gaming." stringByReplacingOccurrencesOfString:#"I" withString:#""];
newString = [newString stringByReplacingOccurrencesOfString:#"like" withString:#""];
newString = [newString stringByReplacingOccurrencesOfString:#"and" withString:#""];
NSLog(#"%#", newString);
More efficient and maintainable than doing a bunch of stringByReplacing... calls in series:
NSSet* badWords = [NSSet setWithObjects:#"I", #"like", #"and", nil];
NSString* str = #"I like Programming and gaming.";
NSString* result = nil;
NSArray* parts = [str componentsSeparatedByString:#" "];
for (NSString* part in parts) {
if (! [badWords containsObject: part]) {
if (! result) {
//initialize result
result = part;
}
else {
//append to the result
result = [NSString stringWithFormat:#"%# %#", result, part];
}
}
}
It is an old question, but I'd like to show my solution:
NSArray* badWords = #[#"the", #"in", #"and", #"&",#"by"];
NSMutableString* mString = [NSMutableString stringWithString:str];
for (NSString* string in badWords) {
mString = [[mString stringByReplacingOccurrencesOfString:string withString:#""] mutableCopy];
}
return [NSString stringWithString:mString];
Make a mutable copy of your string (or initialize it as NSMutableString) and then use replaceOccurrencesOfString:withString:options:range: to replace a given string with #"" (empty string).