How to remove characters from string when string length is unknown - ios

I am taking input from textfield ,i has to extract the characters more than length 5. Then how to remove characters from text

You can try this,
if(level_label.text.length>20)
{
NSString *str=[level_label.text substringFromIndex:20];
// Place str in next_label.text
}

Related

Check if String contains alphanumeric

I want to check the string if it contains alphanumeric only.(Both letters and number only).
I tried using NSCharacterSet *strCharSet = [NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"];
I also tried this NSString *string = #"[^a-zA-Z0-9]"; = no luck
but it only checks if the string does not contain any characters or it contains only this characters. I need to required the user to put both letters and numbers.
-update
I have a textfield for password. I need the user to input both letters and password. But I can't check if the textfield contains BOTH letters and numbers. By using the code above, it only checks if the textfield's text contains only alphanumeric.
You just need to make your checks for numbers and letters separate.
BOOL containsLetter = NSNotFound != [input rangeOfCharacterFromSet:NSCharacterSet.letterCharacterSet].location;
BOOL containsNumber = NSNotFound != [input rangeOfCharacterFromSet:NSCharacterSet.decimalDigitCharacterSet].location;
NSLog(#"Contains letter: %d\n Contains number: %d", containsLetter, containsNumber);

Removing last digit from a string containing numbers

I simply need to remove the last digit from a string that contains numbers.
Say that my mutable string is named scannedItem and produces the following 038000768625.
How can I remove the last digit, and put it into a new string that produces 03800076862?
Thanks
Create a new string from old one. Firstly by removing last character
NSString *newString = [scannedItem substringToIndex:[scannedItem length]-1];
Now append
//Append string of your choice
newString = [newString stringByAppendingString:#"?"];

Printing the length of a string from a label

I am currently trying to check the length of a label.
What it is is i want the label to display "Unavailable" if the string is of a null value.
The sting is being read in from a XML sheet so i don't know what the length of the actual string is and i would like to know. This is what i currently have but its not working.
It displays the relevant text if its not empty which is brilliant.
But its not displaying the text when it is empty which is leading me to believe although i assume its empty its not.
Thanks in advance.
if ([l_subCommunity.text length] > 0)
{
[l_subCommunity setText:_property.str_subCommunity];
NSLog(#"%",l_subCommunity);
}
else
{
NSMutableString *sub = [[NSMutableString alloc]init];
[sub appendString:#"Unavailable"];
[self.l_subCommunity setText:sub];
}
[l_subCommunity setText:_property.str_subCommunity];
[self.l_subCommunity setText:sub];
you are using l_subCommunity setText in the if and self.l_subCommunity setText in the else. are you using 2 different variables?
Also why are you creating a mutable string to pass in the value #"Unavailable" ?
why not simply:
[self.l_subCommunity setText: #"Unavailable" ];
Your if statement is checking the wrong variable. You want:
if (_property.str_subCommunity.length) {
l_subCommunity.text = _property.str_subCommunity;
NSLog(#"%",l_subCommunity);
} else {
self.l_subCommunity.text = #"Unavailable";
}
Also keep in mind that you may end up with whitespace and/or newlines in your string as a result of parsing the XML file. You may need to trim this whitespace from your string.

NSString's method 'getCString' return false for Arabic text

I am generating QR code and everything is working fine if text is only in English. When i want to generate QR code with some Arabic text then it fails at NSString's method "getCString:maxLength:encoding:".
Suppose, I have two strings:
NSString *englishText = #"Some text English";
NSString *englishArabicMixText = #"Some text بالعربي";
char strEng [[englishText length] + 1];
char strArb [[englishArabicMixText length] + 1];
1- [englishText getCString:strEng maxLength:[englishText length] + 1 encoding:NSUTF8StringEncoding];
2- [englishArabicMixText getCString:strArb maxLength:[englishArabicMixText length] + 1 encoding:NSUTF8StringEncoding];
At Case#1 'getCString' return true and QR code is generated and at Case#2 it return false and failed to generate code.
What should I do, so that in case#2 it should also return true ? Thank you
length returns the number of Unicode characters. You have to use lengthOfBytesUsingEncoding:, which returns the number of bytes required to store the receiver in a given encoding.
NSUInteger arbLength = [englishArabicMixText lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
char strArb [arbLength];
[englishArabicMixText getCString:strArb maxLength:arbLength encoding:NSUTF8StringEncoding];
2 is returning false for either 2 possible reasons:
1) the string cannot be converted with the specified encoding.
2) the buffer to hold the encoded string is too small.
I'd guess (or at least I suggest you to start investigating) problem is nr. 2.
Because as you're converting to UTF8 a single un-encoded character may result in more than one encoded character. An 'A' is a single byte with value 65 but an arabic character or some kind of symbol may require more bytes.
You are assuming your destination buffer requires the same number of bytes as the same number of characters of your NSString
So you should do something like that:
NSUInteger size = [englishArabicMixText lengthOfBytesUsingEncoding : NSUTF8StringEncoding];
if(size>0)
{
size++;
strArb = malloc(size); // NOTE: you should allocate space for your string at runtime!!
[englishArabicMixText getCString:strArb maxLength:size encoding:NSUTF8StringEncoding];
}
You should do the same for the plain english string too.
And I'd reccomend to allocate dinamically at runtime the space for the C string with malloc and then free it when you don't need it anymore.

Objective-C - Remove last character from string

In Objective-C for iOS, how would I remove the last character of a string using a button action?
In your controller class, create an action method you will hook the button up to in Interface Builder. Inside that method you can trim your string like this:
if ([string length] > 0) {
string = [string substringToIndex:[string length] - 1];
} else {
//no characters to delete... attempting to do so will result in a crash
}
If you want a fancy way of doing this in just one line of code you could write it as:
string = [string substringToIndex:string.length-(string.length>0)];
*Explanation of fancy one-line code snippet:
If there is a character to delete (i.e. the length of the string is greater than 0)
     (string.length>0) returns 1 thus making the code return:
          string = [string substringToIndex:string.length-1];
If there is NOT a character to delete (i.e. the length of the string is NOT greater than 0)
     (string.length>0) returns 0 thus making the code return:
          string = [string substringToIndex:string.length-0];
     Which prevents crashes.
If it's an NSMutableString (which I would recommend since you're changing it dynamically), you can use:
[myString deleteCharactersInRange:NSMakeRange([myRequestString length]-1, 1)];
The solutions given here actually do not take into account multi-byte Unicode characters ("composed characters"), and could result in invalid Unicode strings.
In fact, the iOS header file which contains the declaration of substringToIndex contains the following comment:
Hint: Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up composed characters
See how to use rangeOfComposedCharacterSequenceAtIndex: to delete the last character correctly.
The documentation is your friend, NSString supports a call substringWithRange that can shorten the string that you have an return the shortened String. You cannot modify an instance of NSString it is immutable. If you have an NSMutableString is has a method called deleteCharactersInRange that can modify the string in place
...
NSRange r;
r.location = 0;
r.size = [mutable length]-1;
NSString* shorted = [stringValue substringWithRange:r];
...

Resources