![]() ![]() This approach isn’t very efficient but is useful if performance isn’t a major concern. Static string JavaStyleSubstring(string s, int beginIndex, 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: But if the first parameter is not 0, the Java and C# functions give different results. Notice that when the first parameter is 0, the Java and C# functions give the same result. 0:00 / 7:12 Chapter 4: substring () and indexOf () methods J David Eisenberg 6.33K subscribers Subscribe 18K views 3 years ago COMSC075 - Evergreen Valley College (Liang Textbook) The substring. The (int beginIndex, int endIndex) method returns a new string that is a substring of this string. The method throws an IndexOutOfBoundException when the startIndex is less than zero or startIndex is greater than endIndex or endIndex is greater than length of String. The C# call to string u = s.Substring(2,5) means extract 5 characters starting at index 2 so u = “23456”. public String substring (int startIndex, int endIndex): This method returns new String object containing the substring of the given string from specified startIndex to endIndex. The C# call to string t = s.Substring(0,3) means extract 3 characters starting at index 0 so t = “012”. There is a more complex version of substring() that takes both start and end index numbers: substring(int start, int end) returns a string of the chars. The C# version of sub-string takes two int parameters that indicate the inclusive start index and the total length of the resulting string. And the Java call to String u = s.substring(2,5) means extract the characters at so u = “234”. The Java call to String t = s.substring(0,3) means extract the characters at indices so t = “012”. For example, suppose string s has 10 characters and is “0123456789”. The Java version of sub-string takes two int parameters that indicate the inclusive start index and the exclusive end index. For example, both languages have sub-string functions to extract a portion of a string, but the functions are slightly different. ![]() The two languages have many similarities but there are quite a few differences to deal with. Returns a new string that is a substring of this string, string buffer, or string builder.The first integer argument specifies the index of the first character. It’s not uncommon for me to port C# code to Java, or Java code to C#. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |