ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How to get the last character of a string?

user-image
تم إضافة السؤال من قبل Zaid Rabab'a , Technical Team Lead , ESKADENIA Software
تاريخ النشر: 2013/07/17
Jamila Yasmeen
من قبل Jamila Yasmeen

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
من قبل 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
من قبل Andrew Francis , Senior Software Developer , OMS

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

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

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

Osama Gamal
من قبل Osama Gamal , Software Consultant , Ministry of Interior

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

Lubna Khan
من قبل 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
من قبل 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

mohammad sonbati
من قبل mohammad sonbati

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

مستخدم محذوف‎
من قبل مستخدم محذوف‎

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

المزيد من الأسئلة المماثلة