Remove the first 4 letters from string - iphone

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);

Related

ios - getting part of an NSString

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.

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!

Formatting a float number inside a Mutable string

I have a couple of floats and they all are stored in a mutable string. But while displaying them it's showing 2011.00000 inspite of 2011 so i want it to format it.
Any suggestions.
Thanks
NSString* str = [NSString stringWithFormat:#"%.f", float_number];
Could you just put them into floats and then use a stringWithFormat(#"%f4.0", ?
Use
NSString* sa = [NSString stringWithFormat: #"%0.0f", 22323.4342];

Truncate white space in text in iphone/objective c

Is there any function to remove the white spaces from text message in objective c?
For eg:for "How are you",the result should be "howareyou"
Thanks in advance.
You could use NSString's componentsSeparatedByCharactersInSet with whitespaceCharacterSet to first split the string on the whitespace, and then join the components using NSArray's componentsJoinedByString.
eg.
NSString *myString=#"How are you";
myString = [[myString componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: #""];
NSLog(myString); // displays Howareyou
Tom's approach isn't very efficient. What you want is:
-stringByTrimmingCharactersInSet:
NSString *myString = #"How are you";
myString = [myString stringByReplacingOccurrencesOfString:#" " withString:#""];

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";