Convert a string/integer to superscript in C# - c#

Is there a built in .NET function or an easy way to convert from:
"01234"
to:
"\u2070\u00B9\u00B2\u00B3\u2074"
Note that superscript 1, 2 and 3 are not in the range \u2070-\u209F but \u0080-\u00FF.

EDIT: I hadn't noticed that the superscript characters weren't as simple as \u2070-\u2079. You probably want to set up a mapping between characters. If you only need digits, you could just index into a string fairly easily:
const string SuperscriptDigits =
"\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079";
Then using LINQ:
string superscript = new string(text.Select(x => SuperscriptDigits[x - '0'])
.ToArray());
Or without:
char[] chars = text.ToArray();
for (int i = 0; i < chars.Length; i++)
{
chars[i] = SuperscriptDigits[chars[i] - '0'];
}
string superscript = new string(chars);

Related

Inserting dashes into string

I would like to know how I could insert dashed into various points into a string?
I have a string that is 32 characters long
I need dashes in various places
8-4-4-4-12
8 characters - 4characters... and so on till string is complete.
I tried REGEX but can only get it to add dashes at regular intervals
string newString = Regex.Replace(currentEPC, ".{6}", "$0-");
I am trying to parse a string as a Guid, but my string does not contain dashes, which it needs to be converted to the Guid.
I am trying to parse a string as a Guid, but my string does not contain dashes, which it needs to be converted to the Guid.
No, it doesn't:
Guid g = Guid.Parse("084c1bfd133d403384e1c02113b52f8c");
This will parse the GUID for you. If you want to have it in a string representation with dashes:
string s = g.ToString();
As Patrick has shown, you don't need to insert the dashes to parse the string to Guid.
However, if you need this method anyway you can use:
public static string InsertStrings(string text, string insertString, params int[] rangeLengths)
{
var sb = new StringBuilder(text);
var indexes = new int[rangeLengths.Length];
for (int i = 0; i < indexes.Length; i++)
indexes[i] = rangeLengths[i] + indexes.ElementAtOrDefault(i-1) + insertString.Length;
for (int i = 0; i < indexes.Length; i++)
{
if(indexes[i] < sb.Length)
sb.Insert(indexes[i], insertString);
}
return sb.ToString();
}
Usage:
string guidString = "36b1dbc650c6407098247f87790144ff";
guidString = InsertStrings(guidString, "-", 8, 4, 4, 4, 12);

C# Replacing Numbers With Symbols

I have an array where user inputs random characters, and i need to replace all numbers with symbol "*". And the worst thing is, that i cant use built in functions! If you can, help please!
Here if char.Number is build in function you should use numbers values from ASCII TABLE for the numbers.
string input = "ArrayWithR23andomChar44acter3sWit55hNumbersI6nIt";
char[] array = input.ToCharArray();
for(int i=0; i < array.Length; i++)
{
if (!char.IsNumber(input[i]))
continue;
array[i] = '*';
}
Here without char.IsNumber you can do it like this:
string input = "ArrayWithR23andomChar44acter3sWit55hNumbersI6nIt";
char[] array = input.ToCharArray();
for(int i=0; i < array.Length; i++)
{
if ((int)input[i] >= 48 && (int)input[i] <=57)
{
array[i] = '*';
}
}
Basically array of characters is nothing than string. You can use this regex to do the job done. For example:
string test = "dsad54dsads56dasd7a8s 5468sda";
Regex:
string t1 = Regex.Replace(test, "[0-9]+", "*");
or
string t1 = Regex.Replace(test, "[0-9]", "*");
The difference is that the first one will replace all consecutive numbers with just one *. The second one will replace every single number with *.
Or, if regex is considered as built in function you can use something like this:
char[] t2 = test.Select(c =>
{
if (c >= '0' && c <= '9')
{
return '*';
}
return c;
}).ToArray();

c# For every 7 letters in the string add item string to Listbox

ListBox box = GetListBox(); //placeholder for the sample
string s = "123456776543219898989";
char[] c = s.ToCharArray();
for(int i=0;i<c.Length;i+=7)
{
box.Items.Add(new string(c, i, 7));
}
This is fast way to separate text.
you can do an easy for loop
ListBox box = null;//set it yourself
for(int i = 0; i < s.Length; i+= 7)
{
box.Items.Add(s.SubString(i, Math.Min(s.Length - i, 7));
}
Break the string into a character array, and use that to create your items. This string constructor overload will help:
http://msdn.microsoft.com/en-us/library/ms131424.aspx
This code is just a sample. What you actually need will depend on how you want to handle the situation where the number of characters in the original string is not evenly divisible by 7.
ListBox box = GetListBox(); //placeholder for the sample
string s = "123456776543219898989";
char[] c = s.ToCharArray();
for(int i=0;i<c.Length;i+=7)
{
box.Items.Add(new string(c, i, 7));
}
I could also do this directly on the string, instead of creating the array, but this should be much faster than calling .SubString() repeatedly.
var str = "123456776543219898989";
int count = 0;
var parts = str.GroupBy(_ => count++ / 7)
.Select(x => string.Concat(x))
.ToArray();
listBox1.Items.AddRange(parts);

Char array wont print at indexes

I have a char array that print A-Z if i convert it to a string but when I try to get a character from an index location I get nothing? ..
char[] codes= new char[156];
for (int i = 65; i < 91; i++) codes[i] = (char)i;
Console.WriteLine(codes[2]);
Because you're starting to store the char's at index 65..
Console.WriteLine(codes[65]); // A
It has to do with the fact that you created a gigantic array but only are populating a small section of it and indexing into a portion that contains no data.
You could consider simplifying using a range..
var chars = Enumerable.Range(65, 90).Select(c => (char)c).ToArray();
Console.WriteLine(chars[2]);
Or you can modify your code to this.
char[] codes = new char[26];
for (int i = 0; i < 26; i++)
{
codes[i] = (char)i;
codes[i] += 'A';
}
Console.WriteLine(codes[2]);
You try the below code
Console.WriteLine(codes[2].ToString());
The reason is Console.WriteLine can display only the string value.

Covert string to hexdecimal string with format "\x..\x.."?

I'm trying to convert a string with modified GUID (e.g. 6b5737e5728786794fff5e009d74d706) to a hex string with format like \x..\x..
(String format and hex chars doesn't work for me). Any ideas?
Regex.Replace(myString, ".{2}", "\\x$0");
If you want to go a non-regex route, then the following might work:
string s = "6b5737e5728786794fff5e009d74d70";
var sb = new StringBuilder($s.Length * 2);
for (int i = 0; i < s.Length; i+=2)
sb.Append("\\x").Append(s.Substring(i, [Math]::Min(2, s.Length - i)));
string myNewString = sb.ToString();
If you want your string to contain (for your example) the code points U+006B, U+0057, U+0037, &c. then think again please. Strings are no byte containers, they are text containers. You want a byte[] in that case:
byte[] byteArray = new byte[(s.Length + 1) / 2]
for (int i = 0; i < s.Length; i+=2)
byteArray[i/2] = Convert.ToByte(s.Substring(i, [Math]::Min(2, s.Length - i)));

Categories