John Walker
John Walker

Reputation: 1121

Android Copy String Like Delphi Copy's Function

Example 1:
i Have an Apple
Example 2:
I Love U

i would like to copy 10 string only, start index from first character. Delphi code would be like this :
copy('I Have an Apple',0,10)
copy('I Love U',0,10)

Result become
i Have an
I Love U

Any same function in Android? no method how long the string, i just want the first 10 character

Upvotes: 1

Views: 814

Answers (1)

Juan Cortés
Juan Cortés

Reputation: 21112

You're probably after .substring(int start,int end) which applied to your code, would be something like the code below, however when you say you would like to copy "10 string only" you should say 10 characters from said string:

String apple     = "i Have an Apple";
String appleCopy = apple.substring(0,10); // "i Have an "

If you want to handle the IndexOutOfBoundsException inline, you could do this, as suggested here, which would take the first n characters if the length is sufficient, or the whole string if its shorter.

String appleCopy = apple.substring(0, Math.min(apple.length(), 10));

Upvotes: 1

Related Questions