C# equivalent to Java's charAt()?
You can index into a string in C# like an array, and you get the character at that index.
Example:
In Java, you would say
str.charAt(8);
In C#, you would say
str[8];
Similar function charAt of Java in C#
There is probably somthing wrong wth your array initialization.
Here is a working example with the same syntax as yours:
string[] elementMath = new [] {"aa", "bb", "cc"};
char result = elementMath[1][0]; // result is 'b'
What is the nearest equivalent to Java's Character.isDefined, in .NET?
You could try with
int utf32 = 0x1FFFF;
string surrogate = Char.ConvertFromUtf32(utf32);
var isDefined = char.GetUnicodeCategory(surrogate, 0) != UnicodeCategory.OtherNotAssigned;
You can use char.GetUnicodeCategory(char)
directly if you have a character from the base BMP.
Note that each version of .NET supports different releases of Unicode, so what it will return is dependant of the version of Unicode used by the current version of .NET/current OS.
Equivalent of C# code for encoding in java
You can use the following method:
Character.toString((char)27);
Java Character.MATH_SYMBOL to C# conversion
char plus = '+';
if(char.GetUnicodeCategory(plus) == UnicodeCategory.MathSymbol)
{
//return true
}
You need to use UnicodeCategory
Be aware not every symbol which can be used in Math is in UnicodeCategory.MathSymbol. * and / are not part of MathSybols
char a2 = '*';
if(char.GetUnicodeCategory(a2) == UnicodeCategory.OtherPunctuation)
{
//return true, but return false for Math.
}
Here how it looks like the unicode character for multiplication and division used in MathSymbols.
char multiplication = '×';
char division = '÷';
Here this should be List of characters in unicode designation Sm(symbol,math)
Java Equivalent of IEnumerablechar
Would a simple String work?
static final String BigAlphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String implements CharSequence, which seems to be what you need, and it also has loads of methods to check membership and such.
CodePointAt equivalent in c#
The exact translation would be this :
string a = "ABC⤶"; //Let's throw in a rare unicode char
Console.WriteLine(a.Length);
for (int n = 0; n < a.Length; n++)
Console.WriteLine((int)a[n]); //a[n] returns a char, which we can cast in an integer
//final result : 4 65 66 68 10550
In C# you don't need codePointAt
at all, you can get the unicode number directly by casting the character into an int
(or for an assignation, it's casted implicitly). So you can get your cp simply by doing
cp = (int)str[n];
How much I love C# :)
However, this is valid only for low Unicode values. Surrogate pairs are handled as two different characters when you break the string down, so they won't be printed as one value. If you really need to handle UTF32, you can refer to this answer, which basically uses
int cp = Char.ConvertToUtf32(a, n);
after incrementing the loop by two (because it's coded on two chars), with the Char.IsSurrogatePair()
condition.
Your translation would then become
string a = "ABC\U0001F01C";
Console.WriteLine(s.Count(x => !char.IsHighSurrogate(x)));
for (var i = 0; i < a.Length; i += char.IsSurrogatePair(a, i) ? 2 : 1)
Console.WriteLine(char.ConvertToUtf32(a, i));
Please note the change from s.Length()
to a little bit of LINQ for the count, because surrogates are counted as two chars. We simply count how many characters are not higher surrogates to get the clear count of actual characters.
C# char.GetNumericValue v/s Java Character.getNumericValue
Different functions in different languages doing different things. The key difference here is that the C# function is specifically for numeric chars, whilst the Java function seems more general purpose.
Java - getNumericValue
Returns the int value that the specified Unicode character represents.
C# - GetNumericValue
Converts a specified numeric Unicode character to a double-precision
floating-point number.
The numeric value of c if that character represents a number; otherwise, -1.0.
Perhaps you are looking for:
C# - Convert.ToInt32
Converts the value of the specified Unicode character to the
equivalent 32-bit signed integer.
Related Topics
Linq to SQL: Multiple Joins on Multiple Columns. Is This Possible
Getting HTML Body Content in Winforms Webbrowser After Body Onload Event Executes
Firefox Browser Does Not Reload the Update CSS/Js Files
How to Flatten an Expandoobject Returned via JSONresult in ASP.NET MVC
Stroke Width Transform (Swt) Implementation (Java, C#...)
How to Access Winform Textbox Control from Another Class
How to Get the "Friendly" Os Version Name
Why Is the Iteration Variable in a C# Foreach Statement Read-Only
Custom Deserialization Using JSON.Net
How to Log the Generated SQL from Dbcontext.Savechanges() in My Program
How to Set HTML to Clipboard in C#
Send Push to Android by C# Using Fcm (Firebase Cloud Messaging)
Asp.Net-Mvc: Razor '@' Symbol in Js File
C# Equivalent to Java's Charat()