How can I encode convert spaces to   in objective-c?
I need each space is replaced by  , except when only one space
Example
in text: AAA BBB C
out text: AAAnbsp;nbsp;nbsp;nbsp;BBB C
thanks
Assuming you're talking about NSStrings -- look at the NSString documentation. There are instance methods for replacing strings of the form stringByReplacing...
Given that you use NSString or NSMutableString:
myString = [myString stringByReplacingOccurrencesOfString:#" " withString:#" "];
myString = [myString stringByReplacingOccurrencesOfString:#" " withString:#" "];
Yes, this needs to be done twice for the case of an odd number of spaces in a row.
If you are dealing with C-strings instead of NSStrings or NSMutableStrings, you do the same but with a function instead of a method. I am not going to write such a function here but know that you have to run it twice. :)
Related
When trying to append to an NSMutableString with appendFormat - It adds spaces.
NSM is just an NSMutableString, att_1_variable & att_2_variable is NSString
[NSM appendFormat:#"<tagname att_1=\" %# \" att_2=\" %# \">", att_1_variable, att_2_variable];
The result is:
<tagname myattribute=" ContentOfVariable " title=" ContentOfVariable ">
Before passing in the strings I am doing:
NSString* att_1_variable = [att_1_variable_orginal stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Is there any way around this?
Thanks
Regards
Christian
You're adding the spaces yourself, by including them in the format string. In C the escape sequence for a quotation mark is just \", with no trailing (or leading) space. So you want:
[NSM appendFormat:#"<tagname myattribute=\"%#\" title=\"%#\">",
attributeVariable, titleVariable];
If there are spaces between the quotation marks and the variable contents after that then your input variables are padded with spaces. You can trim those with something like:
NSString *trimmedAttributeVariable = [attributeVariable
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
...
[NSM appendFormat:#"<tagname myattribute=\"%#\" title=\"%#\">",
trimmedAttributeVariable, ...
Which will trim spaces and tabs from both ends.
I presume you want the result to be
<tagname myattribute="ContentOfVariable" title="ContentOfVariable">
In that case, remove the excess spaces that were around the format specifiers as such:
[NSM appendFormat:#"<tagname myattribute=\"%#\" title=\"%#\">", attributeVariable, titleVariable];
I was wondering how to add whitespaces inbetween letters/numbers in a string with Objective-C.
I have the sample code kinda working at the moment. Basically I want to turn "West4thStreet" into "West 4th Street".
NSString *myText2 = #"West4thStreet";
NSString *regexString2 = #"([a-z.-][^a-z .-])";
for(NSString *match2 in [myText2 componentsMatchedByRegex:regexString2 capture:1L]) {
NSString *myString = [myText2 stringByReplacingOccurrencesOfString:match2 withString:#" "];
NSLog(#"Prints out: %#",myString); // Prints out: Wes thStreet // Prints out: West4t treet
}
So in this example, it's replacing what I found in regEx (the "t4" and "hS") with spaces. But I just want to add a space inbetween the letters to separate out the words.
Thanks!
If you wrap parts of your regex patterns in parentheses, you can refer to them as $1, $2, etc in your replacement string (patterns are numbered from left to right, by the order of their opening parenthesis).
NSString *origString = #"West4thStreet";
NSString *newString = [origString stringByReplacingOccurrencesOfRegex:#"(4th)" withString:#" $1 "];
Not sure I understand your broader use case, but that should at least get you going...
I have a word i.e., P roduct Name in a textfield . we have to trim the space between "P" and "r".
Can you please suggest the solution for this.
Thanks in advance.
There is no real reliable solution for this - there is not enough information provided.
(Unless of course you iterated through a word list, which would be, well, horribly inefficient)
P and r could be separate words for all the computer cares..
This can help you to remove spaces between two words.
NSString *string = #"P roduct Name";
NSString *secondString = [string stringByReplacingOccurrencesOfString:#" " withString:#""];
Output: ProductName
If You want to remove white spaces at start and end of your string
NSString *s = #" Product Name ";
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Output: 'Product Name'
In Xcode, if I have an NSString containing a number, ie #"12345", how do I split it into an array representing component parts, ie "1", "2", "3", "4", "5"... There is a componentsSeparatedByString on the NSString object, but in this case there is no delimiter...
There is a ready member function of NSString for doing that:
NSString* foo = #"safgafsfhsdhdfs/gfdgdsgsdg/gdfsgsdgsd";
NSArray* stringComponents = [foo componentsSeparatedByString:#"/"];
It may seem like characterAtIndex: would do the trick, but that returns a unichar, which isn't an NSObject-derived data type and so can't be put into an array directly. You'd need to construct a new string with each unichar.
A simpler solution is to use substringWithRange: with 1-character ranges. Run your string through a simple for (int i=0;i<[myString length];i++) loop to add each 1-character range to an NSMutableArray.
A NSString already is an array of it’s components, if by components you mean single characters. Use [string length] to get the length of the string and [string characterAtIndex:] to get the characters.
If you really need an array of string objects with only one character you will have to create that array yourself. Loop over the characters in the string with a for loop, create a new string with a single character using [NSString stringWithFormat:] and add that to your array. But this usually is not necessary.
In your case, since you have no delimiter, you have to get separate chars by
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
or this one
- (unichar)characterAtIndex:(NSUInteger) index inside a loop.
That the only way I see, at the moment.
Don't know if this works for what you want to do but:
const char *foo = [myString UTF8String]
char third_character = foo[2];
Make sure to read the docs on UTF8String
I have:
NSString *promise = #"thereAreOtherWorldsThanThese";
which I'm trying to transform into the string:
#"There are other worlds than these"
I'm guessing this is a regex job, but I'm very new to Objective C, and have so far had no luck. I would greatly appreciate any help!
I'd use GTMRegex (http://code.google.com/p/google-toolbox-for-mac/), for example:
NSString *promise = #"thereAreOtherWorldsThanThese";
GTMRegex *regex = [GTMRegex regexWithPattern:#"([A-Z])"];
NSLog(#"%#", [[regex stringByReplacingMatchesInString:promise
withReplacement:#" \\1"] lowercaseString]);
As for removing the uppercase letters you can simply use lowercaseString on NSString.
But as for inserting spaces just before an uppercase letter, I would agree that it would be a job for a regex, and sadly, my regex fu is rubbish :)
Without using any libraries you can use this NSString category I posted. Just perform lowerCaseString on the string array.
How do I convert an NSString from CamelCase to TitleCase, 'playerName' into 'Player Name'?