Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How to get the last character of a string?

user-image
Question ajoutée par Zaid Rabab'a , Software Development Team Leader , Al-Safa Co. Ltd.
Date de publication: 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
par 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
par Andrew Francis , Senior Software Developer , OMS

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

Mohamed Samir
par Mohamed Samir , كابتن اوردر , مطعم الأمير
Redouane boudouma
par 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
par saqlain hashmi , Software Engineer , valentia Technologies

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

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

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

Lubna Khan
par 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
par 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)

Utilisateur supprimé
par Utilisateur supprimé

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

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?