Check if string contains 2 numbers - string

if PassField.Text:match("%a+%d%d+") or PassField.Text:match("%d%d+%a+") then
PWValid = true
else
return 'Error1'
end
This is my code so far but it's not too efficent because the string can be like bb1b12 so i would need to detect if the string contains 2 numbers anywhere

Two numbers is "some digits, then some non-digits, then some more digits". You don't need to care about anything else in string. Just use %d compliment - %D - it means exactly opposite of original. Also you probably should use find if you only want to test if string is valid or not.
str:find("%d+%D+%d+")

Related

The two strings as inputs. Check whether one string is a substring of another. Ignore the uppercase/lowercase differences

The two strings as inputs. Check whether one string is a substring of
another. Ignore the uppercase/lowercase differences.
It is an easy question,
largerString = "This is the larger string not the smaller string"
smallerString = "Smaller String"
if largerString.lower().find(smallerString.lower()) != -1:
print("true")
else:
print("false")
You can take the largerString and the smallerString as inputs.
It basically converted them both into lower case and checks if
the lower string is contained within the largerString by trying
to find it, if it finds it then it is contained else it is not.

Int32.TryParse equivalent for String?

I have been doing some homework. The task was to implement System.Int32.TryParse to check if an entered value is a number or something random. I was wondering if there is a built-in method that checks if something entered is a letter and NOT a number. I tried searching google and MSDN in the string type, but with no luck so far. I did write my own implementation, but I was curious.
Tnx
The easiest way to check whether a character is a number is probably to check whether it is in range from '0' to '9'. This works because of the way characters are encoded - the digits are encoded as a sub-range of the char values.
let str = "123"
let firstIsNumber =
str.[0] >= '0' && str.[0] <= '9'
This gives you a bit different behavior than Char.IsDigit because Char.IsDigit also returns true for thigns that are digits in other alphabets, say ႐႑႒႓႔႕႖႗႘႙ I suspect you do not plan to parse those :-).

Algorithms for "shortening" strings?

I am looking for elegant ways to "shorten" the (user provided) names of object. More precisely:
my users can enter free text (used as "name" of some object), they can use up to 64 chars (including whitespaces, punctuation marks, ...)
in addition to that "long" name; we also have a "reduced" name (exactly 8 characters); required for some legacy interface
Now I am looking for thoughts on how to generate these "reduced" names, based on the 64-char name.
With "elegant" I am wondering about any useful ideas that "might" allow the user to recognize something with value within the shortened string.
Like, if the name is "Production Test Item A5"; then maybe "PTIA5" might (or might not) tell the user something useful.
Apply a substring method to the long version, trim it, in case there are any whitespace characters at the end, optionally remove any special characters from the very end (such as dashes) and finally add a dot, in case you want to indicate your abbreviation that way.
Just a quick hack to get you started:
String longVersion = "Aswaghtde-5d";
// Get substring 0..8 characters
String shortVersion = longVersion.substring(0, (longVersion.length() < 8 ? longVersion.length() : 8));
// Remove whitespace characters from end of String
shortVersion = shortVersion.trim();
// Remove any non-characters from end of String
shortVersion = shortVersion.replaceAll("[^a-zA-Z0-9\\s]+$", "");
// Add dot to end
shortVersion = shortVersion.substring(0, (shortVersion.length() < 8 ? shortVersion.length() : shortVersion.length() - 1)) + ".";
System.out.println(shortVersion);
I needed to shorten names to function as column names in a database. Ideally, the names should be recognizable for users. I set up a dictionary of patterns for commonly occuring words with corresponding "abbreviations". This was applied ONLY to those names which were over the limit of 30 characters.

Smalltalk: how to check wether a string contains only numbers?

so basically I have some input possibilities for the user where should only numbers be accepted, otherwise the user will be alerted his input was incorrect.
the input is considered a String when I read it out using a callback.
now I want to check whether the string(which SHOULD contain numbers) actually DOES ONLY contain numbers, but I didnt find a solution implemented already.
i tried
theString isInteger
-is never true for the string
theString asNumber
- ignores letters, but I want to have a clear output wether letters are included in the string or not
theString isNumber
- always false
In Squeak and Pharo, you have the message #isAllDigits that does exactly what you want:
'1233248539487523' isAllDigits "--> true"
You can use a regular expression to check that the string contains only numbers:
theString matchesRegex: '\d+'
or a more complex regular expression to also allow an optional sign and decimal point:
theString matchesRegex: '-?\\d+(\\.\\d+)?'
Unfortunately, I could not locate messages 'isAllDigits' or 'matchesRegex'on Cincom Smalltalk.
However, what you could do is extract a word from the string and convert it to a number using asNumber.
So, if the returned value is 0(zero) it means that either the number is actually a 0(which could e checked with an additional condition) or string did not contain a digit/number.
This should work with many Smalltalks dialects:
(aString detect: [:c| c isDigit not ]) isNil ifTrue: [ "it's a number" ].

function to confirm the presence of both letters and numbers/ Ignoring excedents

So, I'm trying to build up a program with MATLAB according to some indications from my teacher and I came up with some obstacles which would give me a better grade if I could get them right. Here they are:
The user is asked to insert a string but it can't have more than 20 characters. If it does, the excedents will be ignored and the string is saved with the first 20 characters the user inserted. How do I ignore the excedents in a string and save it anyway?
isletter is a function that tells us if the elements are all letters. In this program, the user is asked to insert a string that needs to include both numbers and letters, so that strings with just letters or just numbers are excluded, and then I'll use a while to keep asking for a string with these characteristics.
Could you please help me? This is my first semester with MATLAB. Thank you!
If you want to disallow characters other than letters and numbers (i.e. '/#!' or whitespace) and require that the string they enter has to have at least 1 letter and 1 number, then you can use the ISSTRPROP function (which is more general than ISLETTER) to check for other types of characters. The idea to use INPUTDLG to prompt for the string (as suggested in Aabaz's answer) is a good one, so here's a nice condensed solution using INPUTDLG that achieves what you want:
answer = ''; %# Initialize answer to be an empty string
while any(~isstrprop(answer, 'alphanum')) || ... %# Check for alphanumeric chars
~any(isletter(answer)) || ... %# Check for at least 1 letter
~any(isstrprop(answer, 'digit')) %# Check for at least 1 number
answer = inputdlg('Enter string:'); %# Prompt for input
answer = answer{1}(1:min(20, end)); %# Trim answer to max of 20 chars
end
Note how the functions MIN and END are used to trim the string to 20 characters.
For the first part of your problem you can use the Matlab function inputdlg which prompts a dialog box asking for user input. Then you can trim the input as you like.
For the second part of your problem the function isletter that you mentioned will tell you for each character individually if they are alphabetic letters, so you could sum that result and check if it is between 1 and 19 for example. That will tell you that your string contains both letters and numbers.
Finally, you can put your code inside a while loop and change a variable when your conditions are met so that you can break outside of the loop.
This example code demonstrates this:
tryagain=1;
while(tryagain)
answer=inputdlg('Insert a 20 character string that contains both letters and numbers','User input');
answer=answer{1};
if(numel(answer)>20)
answer=answer(1:20);
end
letters=sum(isletter(answer));
numbers=sum(~arrayfun(#(x)isempty(str2num(x)),answer));
if(letters>0 && numbers>0)
tryagain=0;
end
end

Resources