Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to get the last character of a string?

user-image
Question added by Zaid Rabab'a , Technical Team Lead , ESKADENIA Software
Date Posted: 2013/07/17

in java jcomeau@intrepid:/tmp$ cat test.java; java test abcdef public class test { public static void main(String args[]) { String string = args[0]; System.out.println("last character: " + string.substring(string.length() -1)); } } last character: f OR String str = "India"; System.out.println("last char = "+str.charAt(str.length()-1)); OutPut = a

Ali Raza Syed
by Ali Raza Syed , Virtual Assistant (Online) , PIDC-LC

In PHP it's as follows: substr("testers", -1); // returns "s" from a variable....
substr($string, -1) or by direct string access $string[strlen($string)-1];

Andrew Francis
by Andrew Francis , Senior Software Developer , OMS

in C# string s = "Hello"; string y = s.Substring(s.Length -1);

Mohamed Samir
by Mohamed Samir , كابتن اوردر , مطعم الأمير
Redouane boudouma
by Redouane boudouma , developpeur , direction des statistiques

:in vb "Dim str As String = "valeur Dim lastchar As Char (lastchar = str.Substring(str.Length -1 ( MessageBox.Show(lastchar

saqlain hashmi
by saqlain hashmi , Software Engineer , valentia Technologies

string str = "astr"; string lastChar=str.Substring(str.Length -1,1));

Osama Gamal
by Osama Gamal , Software Consultant , Ministry of Interior

string str = "abcdz"; string lastChar=str.Substring(str.Length -1,1));

Lubna Khan
by Lubna Khan , Software Engineer , Persistent Systems Limited

There are3 ways:

1) Reverse the string, store it in another string, read the first character of this new reversed string.

2) Print the (size-1) index of the string array, ie. str[size-1] (assuming, array index starts from0).

3) Use a combination of PHP end() and key() functions to achieve the same.

Muhammad Hammad Javaid
by Muhammad Hammad Javaid , IT Project Manager , Dubai Islamic Bank

In c# you can use substring and give the index as str.Length -1 Or You can reverse the string after converting it into char array and picks the first character

in vb.net string.indexof(get last ) or supstring(string)

Deleted user
by Deleted user

ASP.Net : C# / VB.Net string mystring = "ABC"; mystring.Substring(Math.Max(0, mystring.Length -1));

More Questions Like This