简体   繁体   English

我如何设法拆分 C# 中的 int 值

[英]How can I manage to split the int value in C#

For this int has the value of "19971998".因为此 int 的值为“19971998”。 I really wanted to split this value into like this "1997 - 1998"??我真的很想把这个值分成这样的“1997 - 1998”??

int index = Convert.ToInt32(radioListBox1.SelectedIndex);
string indexvalue = Convert.ToString(this.radioListBox1.Items[index]);
string input = Regex.Replace(indexvalue, "[^0-9]+", string.Empty);
MessageBox.Show(input);

you can write like:你可以这样写:

int index = Convert.ToInt32(radioListBox1.SelectedIndex);
string indexvalue = Convert.ToString(this.radioListBox1.Items[index]);
string input = Regex.Replace(indexvalue, ".{4}", "$0-");
MessageBox.Show(input);
var s = "19971998";
String.Format("{0}-{1}", s.Substring(0,4), s.Substring(4, 4)));

To make it work with Regex you'd need to do the following:要使其与Regex一起使用,您需要执行以下操作:

string input = Regex.Replace(indexvalue, "^(.{4})", "$1-");

But as someone suggested, it may be even easier to do this one.但正如有人建议的那样,这样做可能更容易。

string input = indexvalue.Insert(4, "-");
    int index = Convert.ToInt32(radioListBox1.SelectedIndex);
    string indexvalue = Convert.ToString(this.radioListBox1.Items[index]);
    string input = Regex.Replace(indexvalue, "[^0-9]+", string.Empty);

The problem was solved, this was missing my code.问题已解决,这缺少我的代码。

    if (input.Length == 8) input = input.Insert(4, " - ");

    MessageBox.Show(input);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM