I want to remove white space from start of word. i.e my first character should not be space and having only one space in between two words .I am trying lots of things but not able to remove space from starting.
My code is:
controller.h
int whitespaceCount;
controller.m
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
if ([string rangeOfCharacterFromSet:whitespace].location != NSNotFound)
{
whitespaceCount++;
if (whitespaceCount > 1)
{
return NO;
}
}
else
{
whitespaceCount = 0;
return YES;
}
}
from above code one space between two words are works perfectly. Please suggest me how i remove starting space. Thanks in advance.
String result = str. trim(); The trim() method will remove both leading and trailing whitespace from a string and return the result.
Use the String. replace() method to remove all whitespace from a string, e.g. str. replace(/\s/g, '') . The replace() method will remove all whitespace characters by replacing them with an empty string.
Removing All Whitespace From a StringreplaceAll() works with regular expressions (regex). We can use the regex character class '\s' to match a whitespace character. We can replace each whitespace character in the input string with an empty string to solve the problem: inputString. replaceAll(“\\s”, “”).
Use the replace() method to remove all whitespace from a string in TypeScript, e.g. str. replace(/\s/g, '') . The replace method takes a regular expression and a replacement string as parameters. The method will return a new string with all whitespace removed.
you can do with method :-
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (range.location == 0 && [string isEqualToString:@" "]) {
return NO;
}
return YES;
}
Or For latest Swift, you can use following:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if range.location == 0 && (string == " ") {
return false
}
return true
}
using this you cannot able To enter whitescpace at starting.
and you can do also like that:-
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
if you want to remove it from your string , you need to work on following
NEW STRING> = [<STRING_CONTAINS WHITESPACE> stringByTrimmingCharactersInSet:whitespace];
NSLog("String without white space = %@",NEW STRING);
Or if you want to remove whitespace entirely from the textfield use following:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (string == " ") {
return false
}
return true
}
And to make this method work, make sure to mark your textfield.delegate = self
trim the text inside a your textfield when you going to use it by
myTextfiled.text = [myTextfiled.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
this will trim the whitespace from the begining as well as at the end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With