Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove WhiteSpace from start of uitextfield

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.

like image 437
Abha Avatar asked Apr 22 '13 08:04

Abha


People also ask

How do I remove a space at the beginning of a string?

String result = str. trim(); The trim() method will remove both leading and trailing whitespace from a string and return the result.

How do I get rid of whitespace?

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.

How do I remove a whitespace character from a 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”, “”).

How do I get rid of extra spaces in TypeScript?

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.


2 Answers

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

like image 79
Nitin Gohel Avatar answered Oct 05 '22 11:10

Nitin Gohel


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

like image 34
ajithmn89 Avatar answered Oct 05 '22 11:10

ajithmn89