clydewinux
clydewinux

Reputation: 515

get last character in a string

I am new to developing windows phone apps. Now I am creating a text messenger app with T9 keyboard, I already made design like the buttons. Now what I want is how can I get the last character in a string? Example the string is "clyde", how can I get the char 'e' from that String? I am using Visual Basic as language.

UPDATE: Got it working now, I used this code:

string s = "clyde";
char e = s(s.Length-1);

Upvotes: 7

Views: 75756

Answers (7)

Claus Jørgensen
Claus Jørgensen

Reputation: 26341

C#:

string clyde = "clyde"; 
char last = clyde[clyde.Length - 1];

VB.NET

Dim clyde as String = "clyde"
Dim last as Char = clyde(clyde.Length - 1)

Upvotes: 10

Luai Hazaima
Luai Hazaima

Reputation: 1

you can use

String.First

String.last

Upvotes: -1

Tom
Tom

Reputation: 151

Dim s As String = "Clyde"

s = Mid(StrReverse(s), 1, 1)

Upvotes: 0

FRocha
FRocha

Reputation: 960

F#:

"input" |> Seq.last             //val it : char = 't'
"input" |> Seq.last |> string   //val it : string = "t"

Upvotes: 0

user3102516
user3102516

Reputation: 97

C#:

String string = "string";
Char lastLetter = string[string.Length - 1];

VB.NET:

Dim sInput As String  = "sInput"
Dim lastLetter As Char = ChrW(sInput.Length - 1)

Upvotes: 3

Derek
Derek

Reputation: 8628

I would do this with linq :-

   string clyde = "Clyde";
   char lastChar = clyde.Last();

Just my preference.

Upvotes: 6

nothrow
nothrow

Reputation: 16168

I'm not sure about which language are you using, but in C# it is done like

string s = "clyde";
char e = s[s.Length-1];

and it is very similar in every language.

Upvotes: 10

Related Questions