Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i check weather the last position of a string is integer or not??

For Example:-

String str = "Hello123";

Now how can i check the last digit "3" is integer or string??

please help
Posted
Updated 1-Nov-15 19:33pm
v2

If your only concern is to check if the last character is a digit or alpha, you can use the method Char.IsDigit[^]

Like
C#
bool isDigit = Char.IsDigit(str, str.Length-1);
 
Share this answer
 
v2
Comments
Suvendu Shekhar Giri 2-Nov-15 1:43am    
5Ed.
It is better solution than mine.
My solution was smething like-
bool isDigit = int.TryParse(str.Substring(str.Length - 1));
George Jonsson 2-Nov-15 1:53am    
Your solution might be better if the OP wants to do something with the integer.
In your case the integer is already available, my solution only provides the test.
Thanks for the upvote.
Palash Sachan 2-Nov-15 1:53am    
working perfectly correct..Thanks you sir
George Jonsson 2-Nov-15 1:55am    
You are welcome.
Try this-
C#
String str = "Hello123";
str = str.Substring(str.Length - 1);
int temp=0;
if(int.TryParse(str,out temp))
   Console.WriteLine("Integer");
else
   Console.WriteLine("Not Integer");


Hope, it helps :)
 
Share this answer
 
Comments
Palash Sachan 2-Nov-15 1:54am    
Nice..this also working very fine..thanks Sir
You can use Char.IsDigitMethod() to test
C#
String str = "pankaj3";
bool b = Char.IsDigit(str,str.Length - 1);
 
Share this answer
 
Comments
Palash Sachan 2-Nov-15 1:55am    
Working fine..thanks sir for help
George Jonsson 2-Nov-15 20:45pm    
And how is your solution different from mine?
[no name] 2-Nov-15 23:37pm    
I did not saw your solution. You edited the solution. Is your solution is same as 1st version?
[no name] 2-Nov-15 23:42pm    
In previous version you did not mention the Char class.
Palash Sachan 2-Nov-15 22:23pm    
nothing different..its the same as yours sir :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900