ios - getting part of an NSString - iphone

I have seen this question that talks about getting the last part of a NSString.
I require a slight variation on this.
How do I get everything after the http://
Would be good if it was almost as simple as 1 line of code:-)
Cheers

NSString *str = #"http://www.abc.com/news/read/welcome-new-gig/03276";
str = [str stringByReplacingOccurrencesOfString:#"http://"
withString:#""];
hope this will help you.....

substringFromIndex:. You'd be wise to do some bounds checking too. Also, I'd advice taking a look at the documentation before asking a question.

NSString *originalString = #"http://google.com";
NSString *substring = [originalString stringByReplacingOccurrencesOfString:#"http://" withString:#""];

May want to actually check that you are using "http://" at the front and not replace it in every instance. How about this:
if([[str substringToIndex:#"http://".length] isEqualToString:#"http://"])
str = [str substringFromIndex:#"http://".length];
That would be more robust and will actually make sure it starts with "http://" and not replace every instance of it.

Related

How to create JSON format string in iphone?

I want to create JSON string like {"search_element": "New York"} this. I used following code for that.
NSString *JSONString = [NSString stringWithFormat:#"{\"search_element\":""\"%#\"""}",
[searchName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
After doing this I am getting value like {"search_element":"New%20York"} this. I want it should be New York instead New%20York. I am not getting How to format it for expected result.Please help me.
Can't you just use it like that?
NSString *JSONString = [NSString stringWithFormat:#"{\"search_element\":""\"%#\"""}", searchName];
So basically without the stringByAddingPercentEscapesUsingEncoding: method
And I don't really understand why do you use that:
#"{\"search_element\":""\"%#\"""}"
I'd do it with less quotation marks:
#"{\"search_element\":\"%#\"}"
Did a quick test:
NSString *test = #"{\"search_element\":\"%#\"}";
NSLog(test, #"New York");
output:
{"search_element":"New York"}
Hope it helps
you can simply call next
NSString *newString = [JSONString stringByReplacingOccurrencesOfString:#"%20" withString:#""];
However look into JSONKit and SBJSON ..they have methods to make valid JSON in iOS

Get special characters in local push notification alert body [duplicate]

I want to have a percentage sign in my string after a digit. Something like this: 75%.
How can I have this done? I tried:
[NSString stringWithFormat:#"%d\%", someDigit];
But it didn't work for me.
The code for percent sign in NSString format is %%. This is also true for NSLog() and printf() formats.
The escape code for a percent sign is "%%", so your code would look like this
[NSString stringWithFormat:#"%d%%", someDigit];
Also, all the other format specifiers can be found at Conceptual Strings Articles
If that helps in some cases, it is possible to use the unicode character:
NSLog(#"Test percentage \uFF05");
The accepted answer doesn't work for UILocalNotification. For some reason, %%%% (4 percent signs) or the unicode character '\uFF05' only work for this.
So to recap, when formatting your string you may use %%. However, if your string is part of a UILocalNotification, use %%%% or \uFF05.
seems if %% followed with a %#, the NSString will go to some strange codes
try this and this worked for me
NSString *str = [NSString stringWithFormat:#"%#%#%#", #"%%",
[textfield text], #"%%"];
uese following code.
NSString *searchText = #"Bhupi"
NSString *formatedSearchText = [NSString stringWithFormat:#"%%%#%%",searchText];
will output: %Bhupi%
iOS 9.2.1, Xcode 7.2.1, ARC enabled
You can always append the '%' by itself without any other format specifiers in the string you are appending, like so...
int test = 10;
NSString *stringTest = [NSString stringWithFormat:#"%d", test];
stringTest = [stringTest stringByAppendingString:#"%"];
NSLog(#"%#", stringTest);
For iOS7.0+
To expand the answer to other characters that might cause you conflict you may choose to use:
- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters
Written out step by step it looks like this:
int test = 10;
NSString *stringTest = [NSString stringWithFormat:#"%d", test];
stringTest = [[stringTest stringByAppendingString:#"%"]
stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]];
stringTest = [stringTest stringByRemovingPercentEncoding];
NSLog(#"percent value of test: %#", stringTest);
Or short hand:
NSLog(#"percent value of test: %#", [[[[NSString stringWithFormat:#"%d", test]
stringByAppendingString:#"%"] stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]] stringByRemovingPercentEncoding]);
Thanks to all the original contributors. Hope this helps. Cheers!

How to combine two strings in Objective-C for an iPhone app

How can I combine "stringURL" and "stringSearch" together?
- (IBAction)search:(id)sender;{
stringURL = #"http://www.websitehere.com/index.php?s=";
stringSearch = search.text;
/* Something such as:
stringURL_ = stringURL + stringSearch */
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:stringURL_]]];
}
Philippe gave a good example.
You can also use plain stringWithFormat: method.
NSString *combined = [NSString stringWithFormat:#"%#%#", stringURL, stringSearch];
This way you can manipulate string even more by putting somethig inbetween the strings like:
NSString *combined = [NSString stringWithFormat:#"%#/someMethod.php?%#", stringURL, stringSearch];
NSString* combinedString = [stringUrl stringByAppendingString: search.text];
NSString * combined = [stringURL stringByAppendingString:stringSearch];
Instead of stringByAppendingString:, you could also use
NSString *combined = [NSString stringWithFormat: #"%#%#",
stringURL, stringSearch];
This is especially interesting/convenient if you have more than one string to append. Otherwise, the stringbyAppendingString: method is probably the better choice.
You can use stringByAppendingString:
stringURL = [#"http://www.websitehere.com/index.php?s="
stringByAppendingString:search.text];
If you want to have some control about the format of the parameter you should assemble
your URL string with
[NSString stringWithFormat:#"http://www.websitehere.com/index.php?s=%#", search.text]
This solution is charming because you can append almost anything which can be inserted into a printf-style format.
I would not have given the answer of such general question.
There are many answers of same type question have already given. First find the answer of your question from existing question.
NSString* myURLString = [NSString stringWithFormat:#"http://www.websitehere.com/index.php?s=%#", search.text];

Remove the first 4 letters from string

Hi I am having a string as G531 Other Dough Products.
Here I want only Other Dough Products, reply me with code.
I tried with substring but that is not working.
Thanks in advance .
You don't actually say what you tried but substringFromIndex:
NSString *str = #"G531 Other Dough Products";
str = [str substringFromIndex:5];
should do the trick.
You'll probably find that you want to use 5 unless you want the leading space in your new string.
NSString* s = [#"G531 Other Dough Products" substringFromIndex:5];
Try using "substringFromIndex" (see documentation)
He He, you can also do this as below,
NSString * newString = [NSString stringWithCString:(const char*)&"G531 Other Dough Products"[5] encoding:NSUTF8StringEncoding];
NSLog(#"%#", newString);

How to concatenate two strings on iPhone?

How to connect string "Hello" and string "World" to "HelloWorld"? Looks like "+" doesn't work.
NSString *string = [NSString stringWithFormat:#"%#%#", #"Hello", #"World"];
NSLog(#"%#", string);
That should do the trick, although I am sure there is a better way to do this, just out of memory. I also must say this is untested so forgive me. Best thing is to find the stringWithFormat documentation for NSString.
How about:
NSString *hello = #"Hello";
NSString *world = #"World";
NSString *helloWorld = [hello stringByAppendingString:world];
If you have two literal strings, you can simply code:
NSString * myString = #"Hello" #"World";
This is a useful technique to break up long literal strings within your code.
However, this will not work with string variables, where you'd want to use stringWithFormat: or stringByAppendingString:, as mentioned in the other responses.
there's always NSMutableString..
NSMutableString *myString = [NSMutableString stringWithString:#"Hello"];
[myString appendString: #"World"];
Note:
NSMutableString *myString = #"Hello"; // won't work, literal strings aren't mutable
t3.text=[t1.text stringByAppendingString:t2.text];
Bill, I like yout simple solution and I'd like to note that you can also eliminate the space between the two NSStrings:
NSString * myString = #"Hello"#"World";