How can I check if a letter in a string is upper case or lower case ?

  • Thread starter Thread starter fniles
  • Start date Start date
F

fniles

In VB.NET 2005 can I check if a letter in a string is upper case or lower
case ?
For example:
I have the following 2 lines:
NQ,Z2003,11/11/2003,1416.5,1420,1402,1411.5
NQ,z2003,11/10/2003,223801,260154
NQ,H2004,11/11/2003,1416.5,1422.5,1406.5,1415
NQ,h2004,11/10/2003,56,1191

Notice the 1st line has uppercase Z (Z2003) and the 2nd line has lowercase z
(z2003).
After I read the line, I would like to find out if the 2nd item in the line
(after NQ) has small letter or uppercase. How can I do that ?

Thanks.
 
There are a number of ways to do this, but here is a simple one if you
always know which particular char. you want to check:

Dim x As String = "NQ,Z2003,11/11/2003,1416.5,1420,1402,1411.5"
Select Case right(left(x,4),1)
Case "z"
'do something
Case "Z"
'do something else
End Select
 
Thanks.
But, it could be z, it could be f, g, h, j, k, m,n,q,u,v,x.
I am trying to avoid having to do case statement for each one of them.
 
In VB.NET 2005 can I check if a letter in a string is upper case or lower
case ?
For example:
I have the following 2 lines:
NQ,Z2003,11/11/2003,1416.5,1420,1402,1411.5
NQ,z2003,11/10/2003,223801,260154
NQ,H2004,11/11/2003,1416.5,1422.5,1406.5,1415
NQ,h2004,11/10/2003,56,1191

Notice the 1st line has uppercase Z (Z2003) and the 2nd line has lowercase z
(z2003).
After I read the line, I would like to find out if the 2nd item in the line
(after NQ) has small letter or uppercase. How can I do that ?

Thanks.

You could use the .ToUpper and the .ToLower methods.

if val = val.ToUpper then ' string is upper case

if val = val.ToLower then ' string is lower case
 
You could use the .ToUpper and the .ToLower methods.

if val = val.ToUpper then ' string is upper case

if val = val.ToLower then ' string is lower case

And in your specific case, use the .SubString method to pick off the
character you need to check,

if val.substring(3, 1) = val.substring(3, 1).tolower then ' is lower
case
 
Hello fniles,

You can check with the following routine:

Private Function IsUpper(ByVal Value As String) As Boolean
Return (Value = Value.ToUpper)
End Sub

Just give it the letter and it will tell you...

If IsUpper(Strings.Mid(MyLine,4,1)) = True Then
MsgBox ("Is UpperCase")
Else
MsgBox ("Is LowerCase")
End If

Best regards,

Martin
 
Just to re-work some of your code to be more .NET Friendly :):

Private Function IsUpper(ByVal Value As String) As Boolean
Return Value = Value.ToUpper
End Sub

Dim theString = "NQ,Z2003,11/11/2003,1416.5,1420,1402,1411.5"
If IsUpper(theString.subString(3,1)) Then
MessageBox.Show("Is UpperCase")
Else
MessageBox.Show("Is LowerCase")
End If
 
Nice code Martin,

The Mid is one of the old VB commands I have always prefered, I missed it in
other languages.

Cor
 
Thank you, all.
I can also use the following:
bMatch = sMonthYear.Substring(0, 1) Like "[A-Z]"
If bMatch Then
'upper case
else
'lower case
end if
 
I can also use the following:
bMatch = sMonthYear.Substring(0, 1) Like "[A-Z]"

Not if you want your app to work correctly in the rest of the world.
Believe it or not, but there are actually more letters than A - Z.


Mattias
 
Mattias,

I never knew that "letter" means in English the same as in "Old Germanic"
languages as Danish and Dutch, I thought that it was in English "character".
While "letter" is what is in our languages "brief" but that is something
quiet different in English.

:-)

Cor

Mattias Sjögren said:
I can also use the following:
bMatch = sMonthYear.Substring(0, 1) Like "[A-Z]"

Not if you want your app to work correctly in the rest of the world.
Believe it or not, but there are actually more letters than A - Z.


Mattias
 
Cor [not sure if you are being facetious],

In English, "character" can mean any character at all, in English or any
other language: alphabetic, numeric, symbol or punctuation. For example,
the ASCII Character Codes are not limited to just alphabetic characters (but
yes, this example is limited to English characters).

"Letter" means just alphabetic characters (English or otherwise) - - A
subset of all characters. Or, it could just mean a document sent via
traditional post.

-Scott
..

Cor Ligthert said:
Mattias,

I never knew that "letter" means in English the same as in "Old Germanic"
languages as Danish and Dutch, I thought that it was in English
"character". While "letter" is what is in our languages "brief" but that
is something quiet different in English.

:-)

Cor

Mattias Sjögren said:
I can also use the following:
bMatch = sMonthYear.Substring(0, 1) Like "[A-Z]"

Not if you want your app to work correctly in the rest of the world.
Believe it or not, but there are actually more letters than A - Z.


Mattias
 
....
Cor [not sure if you are being facetious],

Of course,

However was it facetious, I would not do it in another way to Mattious or
you. I see now that there is in English as well the intent that a letter
can be a single character.

However in my language and I assume the same from Mattias a letter is always
a single character in my idea even when we use it for by instance the Polish
two character letters (I never saw it used as I do here in Dutch). (The
"karakter" exists as well, however less used). Therefore the things I watch
forever is not to write "letter" for a single character, because that error
is easy made.

Thanks for bringing me on the right track with this.

Cor

In English, "character" can mean any character at all, in English or any
other language: alphabetic, numeric, symbol or punctuation. For example,
the ASCII Character Codes are not limited to just alphabetic characters
(but yes, this example is limited to English characters).

"Letter" means just alphabetic characters (English or otherwise) - - A
subset of all characters. Or, it could just mean a document sent via
traditional post.

-Scott
.

Cor Ligthert said:
Mattias,

I never knew that "letter" means in English the same as in "Old Germanic"
languages as Danish and Dutch, I thought that it was in English
"character". While "letter" is what is in our languages "brief" but that
is something quiet different in English.

:-)

Cor

Mattias Sjögren said:
I can also use the following:
bMatch = sMonthYear.Substring(0, 1) Like "[A-Z]"

Not if you want your app to work correctly in the rest of the world.
Believe it or not, but there are actually more letters than A - Z.


Mattias
 
Hmmm, but a "letter" is not just *any* single character, it is an
"alphabetic" single character.


Cor Ligthert said:
...
Cor [not sure if you are being facetious],

Of course,

However was it facetious, I would not do it in another way to Mattious or
you. I see now that there is in English as well the intent that a letter
can be a single character.

However in my language and I assume the same from Mattias a letter is
always a single character in my idea even when we use it for by instance
the Polish two character letters (I never saw it used as I do here in
Dutch). (The "karakter" exists as well, however less used). Therefore the
things I watch forever is not to write "letter" for a single character,
because that error is easy made.

Thanks for bringing me on the right track with this.

Cor

In English, "character" can mean any character at all, in English or any
other language: alphabetic, numeric, symbol or punctuation. For example,
the ASCII Character Codes are not limited to just alphabetic characters
(but yes, this example is limited to English characters).

"Letter" means just alphabetic characters (English or otherwise) - - A
subset of all characters. Or, it could just mean a document sent via
traditional post.

-Scott
.

Cor Ligthert said:
Mattias,

I never knew that "letter" means in English the same as in "Old
Germanic" languages as Danish and Dutch, I thought that it was in
English "character". While "letter" is what is in our languages "brief"
but that is something quiet different in English.

:-)

Cor

"Mattias Sjögren" <[email protected]> schreef in bericht
I can also use the following:
bMatch = sMonthYear.Substring(0, 1) Like "[A-Z]"

Not if you want your app to work correctly in the rest of the world.
Believe it or not, but there are actually more letters than A - Z.


Mattias
 
Back
Top