C# 将指数数字字符串表示形式拆分为幂和指数

C# 将指数数字字符串表示形式拆分为幂和指数,c#,string,type-conversion,C#,String,Type Conversion,我有一些来自资源的字符串,它们是指数形式的,如下所示:2⁴。我想知道是否有办法将其分为: var base = 2; //or even "2", this is also helpful since it can be parsed 及 我在互联网上搜索过,也搜索过,但是我找不到这个案件的解决办法 指数的格式在英语中称为上标。 如果你用那个关键词搜索,你会发现很多与此相关的问题 上标中的数字在Unicode中映射为: 0 -> \u2070 1 -> \u00b9 2 ->

我有一些来自资源的字符串,它们是指数形式的,如下所示:
2⁴。我想知道是否有办法将其分为:

var base = 2; //or even "2", this is also helpful since it can be parsed


我在互联网上搜索过,也搜索过,但是我找不到这个案件的解决办法

指数的格式在英语中称为上标。 如果你用那个关键词搜索,你会发现很多与此相关的问题

上标中的数字在Unicode中映射为:

0 -> \u2070
1 -> \u00b9
2 -> \u00b2
3 -> \u00b3
4 -> \u2074
5 -> \u2075
6 -> \u2076
7 -> \u2077
8 -> \u2078
9 -> \u2079
您可以在字符串中搜索该值:

Lis<char> superscriptDigits = new List<char>(){ 
    '\u2070', \u00b9', \u00b2', \u00b3', \u2074', 
\u2075', \u2076', \u2077', \u2078', \u2079"};

//the rest of the string is the expontent. Join remaining chars.
str.SkipWhile( ch => !superscriptDigits.Contains(ch) ); 
Lis superscriptDigits=new List(){
“\u2070'、\u00b9'、\u00b2'、\u00b3'、\u2074',
\u2075’、\u2076’、\u2077’、\u2078’、\u2079“};
//字符串的其余部分是expontent。连接其余的字符。
str.SkipWhile(ch=>!superscriptDigits.Contains(ch));

您得到了这个想法

您可以使用一个简单的正则表达式(如果您的源代码非常干净):

这将输出:

基数是2,经验值是44


您可以添加数字到上标数字之间的映射,然后从源中选择所有数字(这将是
)和所有其他数字-指数

const string superscriptDigits = "⁰¹²³⁴⁵⁶⁷⁸⁹";
var digitToSuperscriptMapping = superscriptDigits.Select((c, i) => new { c, i })
                                .ToDictionary(item => item.c, item => item.i.ToString());

const string source = "23⁴⁴";

var baseString = new string(source.TakeWhile(char.IsDigit).ToArray());
var exponentString = string.Concat(source.SkipWhile(char.IsDigit).Select(c => digitToSuperscriptMapping[c]));
现在您可以将
base
index
转换为
int
。 您还需要在执行转换代码之前验证输入


甚至在没有映射的情况下:

var baseString = new string(source.TakeWhile(char.IsDigit).ToArray());
var exponentString = string.Concat(source.SkipWhile(char.IsDigit).Select(c => char.GetNumericValue(c).ToString()));

您可以将正则表达式与
字符串一起使用。规范化

var value = "42⁴³";
var match = Regex.Match(value, @"(?<base>\d+)(?<exponent>[⁰¹²³⁴-⁹]+)");

var @base = int.Parse(match.Groups["base"].Value);
var exponent = int.Parse(match.Groups["exponent"].Value.Normalize(NormalizationForm.FormKD));

Console.WriteLine($"base: {@base}, exponent: {exponent}");
var value=“42⁴³";
var match=Regex.match(值,@“(?\d+)()[⁰¹²³⁴-⁹]+)");
var@base=int.Parse(match.Groups[“base”].Value);
var exponent=int.Parse(match.Groups[“exponent”].Value.Normalize(NormalizationForm.FormKD));
WriteLine($“base:{@base},index:{exponent}”);

我会将此标记为答案,因为它看起来是我的案例中最干净的解决方案。谢谢!@meJustAndrew它是第一个回答的,然后被添加了一些铃铛和哨子的复制剪辑…并被忽略:(@sam我知道你在说什么,昨天发生了什么事,对此我很抱歉,但这是最干净的解决方案,也是问题的答案。为了改进你的答案以及如何赢得声誉,请记住,在你回答之后,你可以继续编辑,直到对你来说是最好的。再次,我对此感到抱歉!@sam为什么你认为这是抄袭?提供正常答案需要多一点时间+看看第二个选项-它根本不涉及上标数字映射。@AlekseyL。这只是解释这一点的更直接的方式,不是针对个人的。解决方案的核心在这里都是一样的。就是这样。:)
const string superscriptDigits = "⁰¹²³⁴⁵⁶⁷⁸⁹";
var digitToSuperscriptMapping = superscriptDigits.Select((c, i) => new { c, i })
                                .ToDictionary(item => item.c, item => item.i.ToString());

const string source = "23⁴⁴";

var baseString = new string(source.TakeWhile(char.IsDigit).ToArray());
var exponentString = string.Concat(source.SkipWhile(char.IsDigit).Select(c => digitToSuperscriptMapping[c]));
var baseString = new string(source.TakeWhile(char.IsDigit).ToArray());
var exponentString = string.Concat(source.SkipWhile(char.IsDigit).Select(c => char.GetNumericValue(c).ToString()));
var value = "42⁴³";
var match = Regex.Match(value, @"(?<base>\d+)(?<exponent>[⁰¹²³⁴-⁹]+)");

var @base = int.Parse(match.Groups["base"].Value);
var exponent = int.Parse(match.Groups["exponent"].Value.Normalize(NormalizationForm.FormKD));

Console.WriteLine($"base: {@base}, exponent: {exponent}");