C# subscript string including variable - c#

I know that in C# to write subscript, I should use Unicode
for example
I want to write H2O , I should write
String str = "H"+"\x2082"+ "O"
But I want to put variable type of int instead of 2 in formula
How can I create a string with variable, which is written in subscript?

In Unicode, the subscript digits are assigned consecutive codepoints, ranging from U+2080 to U+2089. Thus, if you take the Unicode character for subscript 0 – namely, '₀' – then you can obtain the subscript character for any other digit by adding its numeric value to the former's codepoint.
If your integer will only consist of a single digit:
int num = 3;
char subscript = (char)('₀' + num); // '₃'
If your integer may consist of any number of digits, then you can apply the same addition to each of its digits individually. The easiest way of enumerating an integer's digits is by converting it to a string, and using the LINQ Select operator to get the individual characters. Then, you subtract '0' from each character to get the digit's numeric value, and add it to '₀' as described above. The code below assumes that num is non-negative.
int num = 351;
var chars = num.ToString().Select(c => (char)('₀' + c - '0'));
string subscript = new string(chars.ToArray()); // "₃₅₁"

This wikipedia article shows you all the unicode codes for super and subscript symbols.
You can simply create a method which maps these:
public string GetSubScriptNumber(int i)
{
// get the code as needed
}
I will give a few hints to help you along:
Unfortunately you can't just do return "\x208" + i so you'll need to do a switch for the numbers 0-9 or add the integer to "\x2080".
If you only need 0-9 then do some error checking that the input is in that range and throw an ArgumentOutOfRangeException
If you need all ints then you may find it easier splitting it up into each digit and getting a char for each of those - watch out for negative numbers but there is a character for subscript minus sign too!
To include the number in your string, you can use something like String.Format:
String.Format("H{0}O", GetSubScriptNumber(i))

Try to escape it
String str = "H" + "\\x2082" + "O";
or use verbose strings
String str = "H" + #"\x2082" + "O";

Maybe we don't understand your question. Several of the answers above seem correct. Does this work?
static string GetSubscript(int value)
{
StringBuilder returnValue = new StringBuilder();
foreach (char digit in value.ToString())
returnValue.Append((char)(digit - '0' + '₀'));
return returnValue.ToString();
}
string waterFormula = "H" + GetSubscript(2) + "0" // H₂O
string methaneFormula = "CH" + GetSubscript(4) // CH₄

Related

Remove non numeric characters from string and cast numbers as an int into an array

I have a string: "ABD1254AGSHF56984,5845fhfhjekf!54685" and I want to loop through each character in this string and find any non numeric character and remove the characters until the next numeric character. i.e. remove all the non numeric characters from my string but these should be an indiciator that the numbers are seperate.
Output as an integer array:
1254
56984
5845
54685
These should be put into an array and converted as integers.
My attempt below but this just puts all the numbers as one rather than splitting them up based on the non numeric characters:
var input = "ABD1254AGSHF56984,5845fhfhjekf!54685";
var numeric = new String(input.Where(char.IsDigit).ToArray());
//This is the output of my attempt: 125456984584554685
You can use Regex to split up your numbers and letters,
string line = "ABD1254AGSHF56984,5845fhfhjekf!54685";
// This splits up big string into a collection of strings until anything other than a character is seen.
var words = Regex.Matches(line, #"[a-zA-Z]+");
// This gives you a collection of numbers...
var numbers = Regex.Matches(line, #"[0-9]+");
foreach (Match number in numbers)
{
Console.WriteLine(number.Value);
}
// prints
1254
56984
5845
54685
Regex Documentation should be read before implementation for better understanding.
If you can use the .NET Framework, there's a great class called System.Text.RegularExpressions.Regex that can be your new best friend.
var input = "ABD1254AGSHF56984,5845fhfhjekf!54685";
var numeric = Regex.Replace(input, #"[^\d]+", "\n").Trim();
What the above method is doing is it is looking for one or more non-decimal characters and replacing it with a carriage return (\n). The Trim() ensures that it removes any leading or trailing carriage returns.
Output:
1254
56984
5845
54685
There is no need for black magic to solve such a problem. You can solve it with a for-loop. It's been there since the begining of programming and is designed for such stuff ;)
check IF the character is a number. Then start collecting the digits
ELSE you hit a boundary and you can convert the collected digits to a single int and clear your temporal digit storage:
the only tricky thing here is that if you have a number at the end (where there is no non-numeric character as boundary afterwards) you need to check whether you hit the final boundary.
var input = "ABD1254AGSHF56984,5845fhfhjekf!54685";
string separateNumber = "";
List<int> collection = new List<int>();
for (int i = 0; i < input.Length; i++)
{
if (Char.IsDigit(input[i]))
{
separateNumber += input[i];
if (i == input.Length -1) // ensures that the last number is caught
{
collection.Add(Convert.ToInt32(separateNumber));
}
}
else if (string.IsNullOrEmpty(separateNumber) == false)
{
collection.Add(Convert.ToInt32(separateNumber));
separateNumber = "";
}
}
You're almost there, all you need is to not pass the results of your LINQ query to a new String() instance. This should work:
var numeric = input.Where(char.IsDigit).ToArray();

How to split string number into separate integers

I'm new in C# and programming at all, and I face the following problem.
How to split a given number, which I recieve as a string, to array of integers in console application?
For example: My input is 41234 and I want to turn it to array of "4", "1", "2", "3" and "4".
I've tried to use standard
Console.ReadLine().Split().Select(int.Parse).ToArray();
But it sets the whole number as content of the first index of the array, does not splits it.
I've also tried to use char[] array, but in some cases it returns the ASCII value of the char, not the value it represents.
If you want to return each character as an integer, you can just treat the string as an IEnumerable<char> (which it is) and you can use a couple of static methods on the char class:
char.IsNumber will return true if the character is a number
char.GetNumericValue will return the numeric value of the character (or -1 for non-numeric characters)
For example:
int[] numbers = "123456_ABC"
.Where(char.IsNumber) // This is optional if you know they're all numbers
.Select(c => (int) char.GetNumericValue(c)) // cast here since this returns a double
.ToArray();
Alternatively, since we know non-numeric characters get a -1 value from GetNumericValue, we can do:
int[] numbers = "123456_ABC"
.Select(c => (int) char.GetNumericValue(c)) // cast here since this returns a double
.Where(value => value != -1) // This is optional if you know they're all numbers
.ToArray();
In both cases above, numbers becomes: {1, 2, 3, 4, 5, 6}
but in some cases it returns the ASCII value of the char, not the value it represents.
It always does that (a string is a sequence of char), but if you're only dealing with integers via characters in the range '0'-'9', you can fix that via subtraction:
int[] values = s.Select(c => (int)(c - '0')).ToArray();
String.Split uses whitespace characters as the default separators. This means that String.Split() will split along spaces and newlines. It won't return individual characters.
This expression :
var ints = "1\n2 345".Split();
Will return :
1 2 345
A string is an IEnumerable<char> which means you can process individual characters. A Char is essentially an Int32 and digits are ordered. This means you can get their values by subtracting the value of 0:
var ints = "12345".Select(c=>c-'0').ToArray();
Or even :
var sum="12345".Select(c=>c-'0').Sum();
Debug.Assert(sum==15);
This Code snapshot will replace all character except numeric to blank and gives you only int array.
string a = "1344te12we3ta";
Regex rgx = new Regex("\\D+");
string result = rgx.Replace(a, "");
int[] intA = Array.ConvertAll(result.ToCharArray(), c => (int)Char.GetNumericValue(c));
There is much solution in C# for this
int[] values = "41234".ToArray().Select(c=> (int)char.GetNumericValue(c)).ToArray();

convert a hex string to corresponding emoji string

I'm trying to create a string with emoji "👱" starting from this string "D83DDC71". For doing that I'm trying to convert the string above in this string "\uD83D\uDC71".
If i use this code it work (textbox shows 👱 as expected):
textbox.Text += "\uD83D\uDC71";
but if i use this it doesn't work (textbox shows exact text "\uD83D\uDC71" instead of single character):
textbox.Text += sender.Code.ToString("X").insert(4, #"\u").insert(0, #"\u");
What is the right way to convert hex representation of an emoji to a corresponding C# string (UTF-16)?
Okay. It seems you have a string which gives the hexadecimal of each of the UTF-16 code units of the character U+1F471 (👱).
Since char represents a UTF-16 code unit, split the string into two 4-character chunks, parse that into an int as hexadecimal, cast each to char and then combine them into a string:
var personWithBlondHair = ""
+ (char)int.Parse("D83DDC71".Substring(0, 4), NumberStyles.HexNumber)
+ (char)int.Parse("D83DDC71".Substring(4, 4), NumberStyles.HexNumber);
As per https://dotnetfiddle.net/oTgXfG
You have a string containing two shorts in hexadecimal form, so you need to parse them first. My example uses an overload of Convert.ToInt16 which also accepts an integer specifying the base of the integers in the string which, in our case, is 16 (hexadecimal).
string ParseUnicodeHex(string hex)
{
var sb = new StringBuilder();
for (int i = 0; i < hex.Length; i+=4)
{
string temp = hex.Substring(i, 4);
char character = (char)Convert.ToInt16(temp, 16);
sb.Append(character);
}
return sb.ToString();
}
Please note that this method will fail if the string's length isn't divisible by 4.
The reason this works:
textbox.Text += "\uD83D\uDC71";
is because you've got a string literal containing unicode character escape sequences. When you compile your program, the compiler replaces these escape sequences with the correct unicode bytes. This is why you cannot just add \u in front of the characters during execution to make it work.
Try this one
string str = "D83DDC71";
string emoji = string.Join("", (from Match m in Regex.Matches(str, #"\S{4}")
select (char) int.Parse(m.Value, NumberStyles.HexNumber)).ToArray());
This will Separate your string 4 by 4 into array of strings. then it will convert each of strings into char. Finally it will Join all the chars into one string as emoji. all in one line.

get measurement value only from string

I have a string which gives the measurement followed the units in either cm, m or inches.
For example :
The number could be 112cm, 1.12m, 45inches or 45in.
I would like to extract only the number part of the string. Any idea how to use the units as the delimiters to extract the number ?
While I am at it, I would like to ignore the case of the units.
Thanks
You can try:
string numberMatch = Regex.Match(measurement, #"\d+\.?\d*").Value;
EDIT
Furthermore, converting this to a double is trivial:
double result;
if (double.TryParse(number, out result))
{
// Yeiiii I've got myself a double ...
}
Use String.Split http://msdn.microsoft.com/en-us/library/tabh47cf.aspx
Something like:
var units = new[] {"cm", "inches", "in", "m"};
var splitnumber = mynumberstring.Split(units, StringSplitOptions.RemoveEmptyEntries);
var number = Convert.ToInt32(splitnumber[0]);
Using Regex this can help you out:
(?i)(\d+(?:\.\d+)?)(?=c?m|in(?:ch(?:es)?)?)
Break up:
(?i) = ignores characters case // specify it in C#, live do not have it
\d+(\.\d+)? = supports numbers like 2, 2.25 etc
(?=c?m|in(ch(es)?)?) = positive lookahead, check units after the number if they are
m, cm,in,inch,inches, it allows otherwise it is not.
?: = specifies that the group will not capture
? = specifies the preceding character or group is optional
Demo
EDIT
Sample code:
MatchCollection mcol = Regex.Matches(sampleStr,#"(?i)(\d+(?:\.\d+)?)(?=c?m|in(?:ch(?:es)?)?)")
foreach(Match m in mcol)
{
Debug.Print(m.ToString()); // see output window
}
I guess I'd try to replace with "" every character that is not number or ".":
//s is the string you need to convert
string tmp=s;
foreach (char c in s.ToCharArray())
{
if (!(c >= '0' && c <= '9') && !(c =='.'))
tmp = tmp.Replace(c.ToString(), "");
}
s=tmp;
Try using regular expression \d+ to find an integer number.
resultString = Regex.Match(measurementunit , #"\d+").Value;
Is it a requirement that you use the unit as the delimiter? If not, you could extract the number using regex (see Find and extract a number from a string).

Regex to get NUMBER only from String

I recieve "7+" or "5+" or "+5" from XML and wants to extract only the number from string using Regex.
e.g Regex.Match() function
stringThatHaveCharacters = stringThatHaveCharacters.Trim();
Match m = Regex.Match(stringThatHaveCharacters, "WHAT I USE HERE");
int number = Convert.ToInt32(m.Value);
return number;
The answers above are great. If you are in need of parsing all numbers out of a string that are nonconsecutive then the following may be of some help:
string input = "1-205-330-2342";
string result = Regex.Replace(input, #"[^\d]", "");
Console.WriteLine(result); // >> 12053302342
\d+
\d represents any digit, + for one or more. If you want to catch negative numbers as well you can use -?\d+.
Note that as a string, it should be represented in C# as "\\d+", or #"\d+"
Either [0-9] or \d1 should suffice if you only need a single digit. Append + if you need more.
1 The semantics are slightly different as \d potentially matches any decimal digit in any script out there that uses decimal digits.

Categories

Resources