intval($var) == strval($var) returns true - string

the given example is really simple so I don't think it needs any explaining.
I couldn't find any references on the docs that can explain this behaviour and I've also found a couple workarrounds for this, so you don't really need to bother finding them (thanks in advance though).
I'd just really like if some1 could explain this..... doesn't make any sense to me:
// comma separated IDs to later use in SQL statement
$var = '10,20,30,40,743,102394';
$multi_intval = intval($var); // same with (int) $var
$multi_string = strval($var); // same with (string) $var
var_dump($multi_intval, $multi_string, $multi_intval == $multi_string);
// result
int(10) string(22) "10,20,30,40,743,102394" bool(true)
how is 10 equal to a 22 strlen string?

I just ran across this looking for another answer, so even though it is old Ill give an answer in case someone else comes across it.
From the php docs here: If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number
Because of this your comparison intval($var) == strval($var) is changed to something like intval($var) == intval(strval($var)) which is of course equal (I don't know what the language is using to change the string to an integer, the above is just visual representation). If you really need to know if they are identical, use ===.

Related

How to compare a string in nodejs?

I have a string which will be in DB like below.
let text = "Hi {#var#}, ghkk{#Var#}"
But from user we will get the value like below.
let text2 = "Hi xya, ghkkhoww"
It can be upto 10 variables. I want to compare both text and text2 and in place of {#var}, if some value is there, then the string is equal, else it is not. Can anybody please give me an idea how to compare this ?
There are a couple of ways here are a few:
String(var1).includes(var2) | Determine if var1 includes var2
String(var1).indexOf(var2) | returns the index if var2 in var1
String(var1).search(var2) | searches for var2 in var1 (best for paragraphs or sentences)
String(var1).equals(var2) | do both strings equal one-another
String(var1).compareTo(var3) | are these strings comparable
var == var2 | do they equal note do not use === which is know as the equivalence operator.
As you can see its completely dependent on the use case. This is a standard problem in CS but, the common rule of thumb would be using the ==, indexOf, and equals. SideBar I guess you could use Regular expressions, commonly known as Regex. It's pretty powerful, with a small learning curve but, you can easily get expressions wrong and cause an edge case where your code is letting scenarios sneak by which can be difficult to find if you don't know what your looking for or how its affecting your code.
Resources:
https://www.w3schools.com/js/js_string_methods.asp

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 :-).

Convert String of words/letters into an Integer

Today I've finally decided to make an account, in hope for some aid in an issue I've spent the last few hours hunting. (I've spent the past couple hours hunting down a response, from Google to here to Unity Answers. Here's everything that I've found so far, which doesn't work.)
What I'm looking for, is to change a string of purely words/letters into an integer. Therefore "Hello World", would be translated into a string of numbers accordingly. This may be surprising, but this is a lot harder than it sounds. I've found a way to do essentially everything but, thus far.
Presumably the best way would be to get the ASCII value of each letter in the string, and put them all together into a single integer. (No sequences or need to separate them, but one single number.) I have no idea where to get started or how to do that, however. Really anything that you think would work, preferably as short-hand and un-bothersome as possible.
To be as clear as possible, I need to take the letter-only variable "example" and transmorph it to be a integer/only a sequence of numbers.
If you're just trying to convert an arbitrary string into a random seed, then why not try randomSeed.GetHashCode()? That will return an int value suitable for setting the seed, which would produce the same number each time the same string is entered.
You can iterate over all characters, get their charCode and chain them together. The first method splits the string into single chars and uses Array.reduce:
var str = 'qwertzuiop';
var num = parseInt(str.split('').reduce(function(a, b) {return a + b.charCodeAt(0);}, '');
The second calls Array.forEach on the string, because it has numerical indices and a length property.
var num = ''; [].forEach.call(str, function(c) {num += c.charCodeAt(0);});
num = parseInt(num);
In stoneaged browsers you have to use for-loops instead.

Find specific string in vector column Matlab

In my program I need to ask the user a string (ex: 'C4') and then obtain the corresponding index to that string on the following vector:
labels=['C2';'C#2';'D2';'D#2';'E2';'F2';'F#2';'G2';'G#2';'A2';'A#2';'B2';'C3';'C#3';'D3';'D#3';'E3';'F3';'F#3';'G3';'G#3';'A3';'A#3';'B3';'C4';'C#4';'D4';'D#4';'E4';'F4';'F#4';'G4';'G#4';'A4';'A#4';'B4';'C5';'C#5';'D5';'D#5';'E5';'F5';'F#5';'G5';'G#5';'A5';'A#5';'B5'];
To ask the string is simple, used input and it worked on octave, but to find the index I couldn't make it work. I tried using find, find(ismember( and also with a for cycle but nothing seems to work.
Appreciate the help
Thanks.
Use strcmp (and note labels should be a cell array, as the strings have different lengths):
labels = {'C2';'C#2';'D2';'D#2';'E2';'F2';'F#2';'G2';'G#2';'A2';'A#2';'B2';'C3';'C#3';'D3';'D#3';'E3';'F3';'F#3';'G3';'G#3';'A3';'A#3';'B3';'C4';'C#4';'D4';'D#4';'E4';'F4';'F#4';'G4';'G#4';'A4';'A#4';'B4';'C5';'C#5';'D5';'D#5';'E5';'F5';'F#5';'G5';'G#5';'A5';'A#5';'B5'};
str = 'C4';
index = find(strcmp(str,labels));
You can use strcmpi instead of strcmp if case is not important.

String matching without using builtin functions

I want to search for a query (a string) in a subject (another string).
The query may appear in whole or in parts, but will not be rearranged. For instance, if the query is 'da', and the subject is 'dura', it is still a match.
I am not allowed to use string functions like strfind or find.
The constraints make this actually quite straightforward with a single loop. Imagine you have two indices initially pointing at the first character of both strings, now compare them - if they don't match, increment the subject index and try again. If they do, increment both. If you've reached the end of the query at that point, you've found it. The actual implementation should be simple enough, and I don't want to do all the work for you ;)
If this is homework, I suggest you look at the explanation which precedes the code and then try for yourself, before looking at the actual code.
The code below looks for all occurrences of chars of the query string within the subject string (variables m; and related ii, jj). It then tests all possible orders of those occurrences (variable test). An order is "acceptable" if it contains all desired chars (cond1) in increasing positions (cond2). The result (variable result) is affirmative if there is at least one acceptable order.
subject = 'this is a test string';
query = 'ten';
m = bsxfun(#eq, subject.', query);
%'// m: test if each char of query equals each char of subject
[ii jj] = find(m);
jj = jj.'; %'// ii: which char of query is found within subject...
ii = ii.'; %'// jj: ... and at which position
test = nchoosek(1:numel(jj),numel(query)).'; %'// test all possible orders
cond1 = all(jj(test) == repmat((1:numel(query)).',1,size(test,2)));
%'// cond1: for each order, are all chars of query found in subject?
cond2 = all(diff(ii(test))>0);
%// cond2: for each order, are the found chars in increasing positions?
result = any(cond1 & cond2); %// final result: 1 or 0
The code could be improved by using a better approach as regards to test, i.e. not testing all possible orders given by nchoosek.
Matlab allows you to view the source of built-in functions, so you could always try reading the code to see how the Matlab developers did it (although it will probably be very complex). (thanks Luis for the correction)
Finding a string in another string is a basic computer science problem. You can read up on it in any number of resources, such as Wikipedia.
Your requirement of non-rearranging partial matches recalls the bioinformatics problem of mapping splice variants to a genomic sequence.
You may solve your problem by using a sequence alignment algorithm such as Smith-Waterman, modified to work with all English characters and not just DNA bases.
Is this question actually from bioinformatics? If so, you should tag it as such.

Resources