Languages
[Edit]
EN

Java - get last char from string

11 points
Created by:
AnnLen
1780

Problem:

How to get last character from string in java?

I have: "abc"
I want: "c"
String str = "abc";
System.out.println(str); // abc
// ..
System.out.println(str); // c

Solution:

String str = "abc";
System.out.println(str.charAt(str.length() - 1)); // c

Docs:
https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#charAt-int-

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.