In high school I started learning programming and practising it in Turbo Pascal. When it comes to strings, there is a method that is implemented in every language, and that is the method that extracts a substring form a given string, based on some parameters.
In Turbo Pascal the method is called Copy and here is the description:
COPY
Syntax: Copy(St, Start, Number)
The Copy function returns a substring copied out of St starting at position Start containing Number characters. Thus St is a string expression and Start and Number are positive integers. If Start > Length(St), the empty string is returned. If Start + Number > Length(St), only characters in the string St are returned. If Start is outside the range 1..255, a run-time error occurs.
So if you have the example string, Copy(“example”,2,4) will result in xamp. (In Turbo Pascal, indexing starts with 1)
The next programming language I was introduced to was C. In C there is a string.h files which contains all functions available for string processing, but not a substring method. So msot programmers use strncpy(click for api) like this
const char* input= “example”;
char *result = (char*) malloc(6);
strncpy(result, from+2, 4);
So the string value referenced by result will be in this case ampl.(In C, indexing starts with 0).
Next there was Php, which has a method called substr(click for api).
< ?php $result = substr(“example”,2,4); // returns “ampl”
And in Java we have a method in the String class called substring.
public String substring(int beginIndex,int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. Thus the length of the substring is endIndex-beginIndex.
Thus doing the following in Java:
String str=”example”;
String result = str.substring(2,4);
Will create a new result string with the value of “am“.
In Javascript there is also a substring method which works like the one in Java.
Anyway, I just wrote this post to express my frustration, because yesterday I went full retard on a Java Recruting Test and and when asked what “example”.substring(2,4) produces, I wrote very sure of myself “ampl“. (WTF BRAIN!?) I am still puzzled by the answer I gave, and I am trying to figure out why my brain computed that answer using programming languages I haven’t used in a while, instead of Java, which I use every day. Of course I realised my mistake when I got into bed, and couldn’t sleep for two hours because my brains was screaming “Stupid, stupid, stupid! How could I be so stupid???”
This is one of the worst brain-farts I had in a while. Oh well, somewhere in this town there is somebody correcting that test and probably thinking I am the worst Java programmer ever. At least I still have a job. :D



