Is there a substring function (start, length)?

I am trying to use a similar method for C # string.SubString(int start, int length) .

But the subscript function in Java is string.substring(int start, int end) .

I want to be able to pass in the starting position and the length of the subscript function.

Can someone help me solve this problem?

+6
source share
3 answers

It could be something like

 String mySubString(String myString, int start, int length) { return myString.substring(start, Math.min(start + length, myString.length())); } 

+18
source

The closest thing you can get is using the String constructor

 new String(input.getBytes(),start,length); 
+1
source

Yes

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int , int)

public String substring (int beginIndex, int endIndex)

you just need to calculate a bit to get endIndex.

0
source

Source: https://habr.com/ru/post/955400/


All Articles