The Java Substring Function vs. the C# Substring Function

It’s not uncommon for me to port C# code to Java, or Java code to C#. The two languages have many similarities but there are quite a few differences to deal with. For example, both languages have sub-string functions to extract a portion of a string, but the functions are slightly different.

The Java version of sub-string takes two int parameters that indicate the inclusive start index and the exclusive end index. For example, suppose string s has 10 characters and is “0123456789”. The Java call to String t = s.substring(0,3) means extract the characters at indices [0, 3) = [0, 2] so t = “012”. And the Java call to String u = s.substring(2,5) means extract the characters at [2, 5) = [2, 4] so u = “234”.

The C# version of sub-string takes two int parameters that indicate the inclusive start index and the total length of the resulting string. The C# call to string t = s.Substring(0,3) means extract 3 characters starting at index 0 so t = “012”. The C# call to string u = s.Substring(2,5) means extract 5 characters starting at index 2 so u = “23456”.

JavaVsCSharpSubstring

Notice that when the first parameter is 0, the Java and C# functions give the same result. But if the first parameter is not 0, the Java and C# functions give different results.

When I’m porting Java code to C#, I often use a program defined C# method that simulates how the Java substring method works so I don’t have to deal with indices:

static string JavaStyleSubstring(string s, int beginIndex,
  int endIndex)
{
  // simulates Java substring function
  int len = endIndex - beginIndex;
  return s.Substring(beginIndex, len);
}

This approach isn’t very efficient but is useful if performance isn’t a major concern.

Advertisements
This entry was posted in Machine Learning, Miscellaneous. Bookmark the permalink.