Posts Tagged trim

How to trim NSString in Objective C 2.0 – iOS, iPad, iPhone

When you want to trim a NSString in Objective C you can use NSString’s method stringByTrimmingCharactersInSet and passing it a [NSCharacterSet whitespaceAndNewlineCharacterSet] characterSet. This will trim your NSString from all newline and whitespace characters.

NSString *trimMe = @"    Trim this string      ";
trimMe = [trimMe stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"Trimmed String: ++%@++", trimMe);
//returnes Trimmed String: ++Trim this string+++

You can also trim just whitespace by passing [NSCharacterSet whitespaceCharacterSet] or trim just newline characters by passing [NSCharacterSet newlineCharacterSet]

Advertisements

, , , , ,

1 Comment

ActionScript 3.0: How to use trim funtion in Flex application

In ActionScript 3.0, we cannot use the trim function on strings, that we love from PHP or JavaScript. The trim function is however available to us if we include StringUtil in our project with import mx.utils.StringUtil.

When we import StringUtil, we can trim any string with StringUtil.trim(string). I love trim function and I was glad to find it in ActionScript.

import mx.utils.StringUtil;

var myString:String = '  This string needs to be trimmed   ';
myString = StringUtil.trim(myString);
trace(myString);
//return's This string needs to be trimmed with no whitespace on the left or right side

, ,

Leave a comment