Feb 06 2013

The substring story

Category: Wild thoughtsseaqxx @ 12:45

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

Tags: ,

3 Responses to “The substring story”

  1. costelush says:

    I don’t have much… recruiting experience, but if someone would ask me in a test what substring does, I would get up and leave.

  2. seaqxx says:

    Oh well, I feel the same way :(, unfortunately this is the world we live in.

  3. bu7ch3r says:

    So…What is the answer in JAVA?
    In the worst case scenario I can figure that “example” is a temporary “string” object and after next line(“of code”) everything is flushed out. But I am thinking in c++ kind of…
    Considering that your call destString = string(“example”).substr(2,4); everything should be fine.
    I don’t get it.

    OOOOOHHH. I’ve just thought in reading your post all the way. So in JAVA is startIndex, endIndex :) COOOOOL :)

    As a conclusion, C, C++, STL, WTL, WPF, PHP, WTF, etc :D do it like this :) You should kill the standard comitee of JAVA :P

    Anyway, I agree, if you are being asked such things as a non-junior, you should leave…as soon as possible, because the person in front of you was:
    1)Missplacing your resumee and he thinks you are a Junior.(Hey junor, bring me a coffee:)))
    2)a complete idiot, and you don’t want to work in a firm where you are better than your Technical Lead
    3)hiring a junior anyway :)

    P.S. 2 moths ago a very “bimbo”/”pitipoanca” recruiter(girl) was telling me that the best job for me is a junior one and that I am indeed a junior(by experience) for that job.
    Oh well, I thought that it was better to reply her using my company mail and my signature(with all the theatrical job names I have there :P), telling her that I have decided to leave the job for a freshmen:D(You can see how I think all the time at the poor little university graduates).

Leave a Reply