Let's sort out the differences between Java substring and C # Substring, and how to port them.

Introduction

I'm porting existing assets from Java to C #, but there is a bug due to a difference in the specification of the method that cuts out a part of the character string, so make a note for the future.

Method to cut out a part of a character string

There are two types of Java and C #. The explanation is a reference citation.

Java String # substring method

Method Description
public String substring(int beginIndex) Returns a string that is a substring of this string. The substring starts at the specified index and ends at the end of this string.
public String substring(int beginIndex, int endIndex) Returns a string that is a substring of this string. The substring starts at the specified beginIndex and endsindex-Up to the character in 1. Therefore, the length of the substring is endIndex-It becomes beginIndex.

C # String # Substring method

Method Description
public string Substring (int startIndex); Get the substring from the instance. The substring starts at the specified character position in the string and continues to the end of the string.
public string Substring (int startIndex, int length); Get the substring from the instance. This substring is a string with a specified number of characters, starting at the specified character position.

Work when porting from Java to C

It may be converted as follows.

Java method Java code before conversion After conversion C#code
public String substring(int beginIndex) "String".substring(beginIndex) "String".Substring(beginIndex)
public String substring(int beginIndex, int endIndex) "String".substring(beginIndex, endIndex) "String".Substring(beginIndex, endIndex - beginIndex)

Work when porting from C # to Java

It may be converted as follows.

C#Method Before conversion C#code 変換後Javacode
public string Substring (int startIndex); "String".Substring(beginIndex) "String".substring(beginIndex)
public string Substring (int startIndex, int length); "String".Substring(beginIndex, length) "String".substring(beginIndex, beginIndex + length)

reference

-Java class String # substring -C # String.Substring method

Recommended Posts

Let's sort out the differences between Java substring and C # Substring, and how to port them.
Summarize the differences between C # and Java writing
Difference between Java and JavaScript (how to find the average)
Differences in how to handle strings between Java and Perl
[Java] Types of comments and how to write them
A memo about the types of Java O/R mappers and how to select them
[Rails] Differences between redirect_to and render methods and how to output render methods
[Java] How to use substring to cut out a character string
Think about the differences between functions and methods (in Java)
How to write comments in the schema definition file in GraphQL Java and reflect them in GraphQL and GraphQL Playground
[Ruby] How to get the value by specifying the key. Differences between hashes, symbols and fetch
Differences between "beginner" Java and Kotlin
Differences between Java and .NET Framework
[Java] How to convert from String to Path type and get the path
[Java] How to get the current date and time and specify the display format
[Java] How to use substring to cut out a part of a character string
How to find out the Java version of a compiled class file
[Java improvement case] How to reach the limit of self-study and beyond
How to make an app with a plugin mechanism [C # and Java]
Java8 / 9 Beginners: Stream API addiction points and how to deal with them
How to create your own annotation in Java and get the value
[Java] How to use the File class
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] Differences between instance variables and class variables
[Java] How to output and write files!
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Processing × Java] How to use the class
How to sort the List of SelectItem
How to install the legacy version [Java]
How to find the tens and ones
How to get the date in java
[Processing × Java] How to use the function
How to use substring and substr methods
[Java] How to use the Calendar class
How to switch between multiple Java versions
Java to C and C to Java in Android Studio
Let's refer to C ++ in the module of AndroidStudio other project (Java / kotlin)
How to find the distance and angle between two points on a plane
[Java] Understand the difference between List and Set
[Java] How to use FileReader class and BufferedReader class
[Java] How to use Thread.sleep to pause the program
[Java] How to get and output standard input
Differences in writing Java, C # and Javascript classes
[Java] (for MacOS) How to set the classpath
How to use the replace () method (Java Silver)
How to get and study java SE8 Gold
[Swift] How to implement the fade-in / out function
http: // localhost: How to change the port number
How to find the total score and average score
[Java] How to get the redirected final URL
[Java] Memo on how to write the source
[Java] How to get the authority of the folder
Rails scope anti-patterns and how to eliminate them
Ruby How to convert between uppercase and lowercase
How to access Java Private methods and fields
[Java] How to use Calendar class and Date class