Hi guy, I’m new in the Csharp programming so please excuse me if my question is a bit silly; I need to check whether a char or a string is Uppercase or not, I googled it but the methed google offers are not available in unity or perhaps just in my computer, it recommends to use IsUpper() and the only methed my Csharp seems to have is IsNormalized(), so please can anybody help me how to check is a letter is Uppercase or Lowercase?
Well a string is a collection of characters, so if you want to check one character, then you are dealing with char
s.
So you want to use char.IsUpper. This is in the System namespace, so make sure you have using System;
at the top of your script.
As a sidenote, most of the things you do need for a bit advanced (or just atypical) C# programming are in the System namespace, or a sub namespace thereof (i.e. System.Collections.Generic and System.Text being among the frequent ones).
If you are absolutely sure your characters are culture invariant (or intend to ignore culture altogether), you can always do the following just as well
if(myString[i] >= 'A' && myString[i] <= 'Z') // I'm uppercase
Notice the single quotes, this is how you address chars which are technically numeric types, strings being the “arrays” of char types (not really really, but it’s all very similar and you get the point). This also works because all letter characters are internally encoded to follow ASCII, you can do the same thing with the lowercase ones and digits as well.
But, by all means, it’s better to do what spiney199 suggested.
Fiddling with chars can help you come up with some useful stuff on the fly.
Just as a show-off you can do your own lowercasing
var s = "I WAS SCREAMING THIS MESSAGE BUT I WAS MUFFLED";
var sb = new StringBuilder(); // you need System.Text for this one
for(int i = 0; i < s.Length; i++) {
if(s[i] >= 'A' && s[i] <= 'Z')
sb.Append((char)(s[i] + 32));
else
sb.Append(s[i]);
}
Debug.Log(sb.ToString());
You can also produce letters on the fly
for(int i = 0; i < 26; i++) Debug.Log((char)('A' + i));
In general, this is how you can categorize the character in a system-supported way
Char.GetUnicodeCategory (UnicodeCategory enum)
If you’re completely new it makes sense to acquaint you with how to build your strings in C# if you ever need programmatic control over such things.
- StringBuilder
Strings are peculiar beasts in C#. Unlike any other type in C# they act like value types but behave like reference types. Strings in C# are immutable, and this is a very important fact to know.
This is why I don’t build that string character by character, even though I could’ve concatenated it with + (or string.Concat). It’s just a good habit to be mindful of garbage in memory, and in that case the code would produce all these strings.
“i”
"i "
“i w”
“i wa”
“i was”
you get the point
99% of these strings would have to be garbage collected and because they’re larger and larger all of this space has to be allocated on the heap, and it’s pretty wasteful. StringBuilder is a native C# manager for doing this kind of thing, and it allocates memory similar to how Lists grow (in steps of 2^n) and writes to it sequentially.
- string interpolation
This is syntactic sugar to help you work with string.Format which was the ubiquitous way to print and debug in human readable form. It’s practically the C# way of doing C’s printf so the core concept hasn’t changed for a lot of decades lol
This is one way to do it
var s0 = "today's date is:";
Debug.Log(s0 + " " + makeStringFromDate(DateTime.Now));
string makeStringFromTodayDate(DateTime date) {
var m = date.Month;
var d = date.Day;
var y = date.Year;
return m.ToString() + "/" + d.ToString() + "/" + y.ToString();
}
But this is horrible, don’t do that. Instead of doing so much work and concatenating literal strings and dynamic data, you produce one string template, for example “today’s date is: {0}” and then you supply variable as an argument. string.Format will call ToString() on this object by default.
Debug.Log(string.Format("today's date is: {0:d}", DateTime.Now)); // you need using System; for this
Observe that :d info in the braces. That’s the actual format I want to be used. There are a loads of premade standardized “Excel-like” formats at your disposal, but it can also be heavily customized. (‘d’ means short date format btw, which excludes the time)
String interpolation improves all of this by letting the compiler transfix a shorter notation to a string.Format invocation because we all constantly need this feature.
Starting from C# 6, the above example can be written as
Debug.Log($"today's date is: {DateTime.Now:d}");
If you have issues with the expression inside the braces, just remember that you can help the compiler by parenthesizing it.
I.e.
var favoriteFruit = FruitEnum.Apple;
Debug.Log($"This person {(favoriteFruit == FruitEnum.Orange? "does" : "doesn't")} like oranges.");
In Unity you perhaps want to be able to fix the number of digits after the decimal point, or you want to show a number with some zero-padding, or something along these lines.
Debug.Log($"{myVector:F4}"); // shows four digits after the decimal point
for(int i = 0; i < 5; i++) {
var score = 656.51125138 * Math.Pow(10, i);
Debug.Log($"{score:0,000,000}"); // #,##0 does the same without zero-padding
}
edit:
Also make sure to search this forum for something called InvariantCulture. It’s a deep topic, but you ought to understand it if you’re serializing or sharing data across the network, and with Unity you’re serializing 100% of the time, so it makes sense to check it out.
This is the kind of stuff I check on SO:
bool result = !myString.Where(char.IsLower).Any();
from: c# - Check if an uppercase Letter is inside a string - Stack Overflow
It’s worth noting that Any() extension is part of System.Linq namespace.
Just a disclaimer:
Using LINQ excessively in one’s project may have terrible consequences to performance.
LINQ is made with boring tabular data and querying in mind, not as a game dev tool.
It tries to optimally scour, sift, and collate through quantities of data in a readable manner.
It does not try to be efficient with frame time, CPU usage, and memory allocation/consumption, in a contextual video game sense.
Which is why SO is usually a horrible place to find a solution for Unity environment.
You are right, however for a one time initialization or after an user input, Linq can be used without worrying too much. I wouldn’t use it in an Update method or for a long initialization process.
Fully agreed. Just a newbie disclaimer.
In general, C# in Unity will do anything that normal C# does, in the normal way. To check for an uppercase letter in a Unity game, find the regular (non-Unity) way C# does it. If it’s something about color or animation or game-y stuff, then you need the special C#+Unity manual.
But it’s the internet, and this forum is also the internet. People will tell you all sorts of strange, wrong and overly-complicated stuff. You may have to look at lots of junk before finding upper-case testing that makes sense. Worse, you may get one that works great, but uses terms you haven’t learned yet, since there’s a lot to learn about coding.