使用Dash将字符串转换为数字的C#问题( - ) [英] C# Problems of Converting String to Number with Dash (-)

查看:100
本文介绍了使用Dash将字符串转换为数字的C#问题( - )的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,这是情景,我有ID号。这是2014-11-0001,我试图将这个文本转换为Int,所以我可以像2014-11-0002那样增加它,有没有办法将DASH转换为Int?请帮助

解决方案

从不同的角度来看,我认为这是在问这样一个问题:我怎么能有一个表现出行为的.NET对象像这样:它将有一个顺序的计数器,并且对于对象的所有实例都是唯一的;它将体现一个DateTime,它将做正确的事情来生成某种类型的格式化字符串结果。



您可以通过以下方式创建一个类:

 使用系统; 

命名空间 YourNameSpace
{
public class DateWithCounter
{
public int ID { set ; get ; }
public DateTime Date { set ; get ; }

public DateWithCounter(DateTime theDate)
{
Date = theDate;
ID = counter ++;
}

// ToString覆盖
< span class =code-keyword> public override string ToString()
{
return string .Format( {0} {1} {2} {3} {4}
Date.Year,
dash,
Date.Month.ToString()。PadLeft( 4 ,零),
破折号,
ID.ToString()。PadLeft( 4 ,零));
}

// 静态元素

private static char dash = ' - ';
私有 静态 char 零= ' 0';
private static int counter = 0 ;
}
}

好的,让我们测试一下这个类:

  private   void  SomeButton_Click( object  sender,EventArgs e)
{
List< DateWithCounter> dtCounters = new List< DateWithCounter>();

DateTime adate;

for int i = 1 ; i < 13 ; i ++)
{
adate = new DateTime( 2015 ,i,i * 2);
dtCounters.Add( new DateWithCounter(adate));

// 测试字符串输出
Console.WriteLine(dc );
}
}

这是输出:

 2015-0001-0000 
2015-0002-0001
2015-0003 -0002
2015-0004-0003
2015-0005-0004
2015-0006-0005
2015-0007-0006
2015-0008-0007
2015-0009-0008
2015-0010-0009
2015-0011-0010
2015-0012-0011

我希望你从这里拿走例如,C#/ .NET为您提供了设计对象/类型的强大工具,并通过巧妙的编码解决了问题。



我还认为开发设计类型/对象的技能导致代码具有更高的封装质量。



但是,我应该声明知道何时到采取特定的编码问题并创建一个小实用程序/工具来解决这个问题,在其他类似问题中,这是一个同样有价值的技能。



在这种特殊情况下,我认为不需要创建工具/实用程序。当然,创建一个具有静态计数器变量的泛型类很容易!

引用:

告诉我,我忘了。教我,我记得。让我和我学习。

Benjamin Franklin


No。

Dash是一个字符串文字,但不是有数字意义,你需要自定义逻辑来完成你的任务

拆分变量用 - 然后将其转换为int然后再增加

见下面的代码段

  string  szValue =   2014-11-0001; 
int iCount = convert.toInt32(szValue.Split(' - ')[ 2 ]);
iCount = iCount + 1 ;
int val = iCount.ToString( D)。长度+ 3 ;
szValue = 2014-11 - + convert.toString(val);



以上逻辑你可以检查价值'9999'并增加另一个值



希望它有帮助


你从错误的一端解决问题。

你必须回答如何建立参考?这个问题。

在你的例如,引用是从一年,一个月和一个计数器构建的。

普通应用程序正在跟踪此引用的组件,并且仅根据请求构建引用。



如果你的应用程序没有跟踪组件,你必须在其组件中拆分引用,然后生成下一个引用。



这看起来像HomeWork,所以开始工作,当你的代码出现问题时回到这里。


Hello, here's the scenario, i have ID no. which is 2014-11-0001, im trying to convert this text to Int, so that i can i can increment it like 2014-11-0002, is there a way to convert DASH to an Int? Please help

解决方案

Looked at from a "different" point of view, I see this as asking the question: "how can I have a .NET object that will behave like this: it will have a counter that is sequential, and is unique for all instances of the object; it will embody a DateTime, and it will 'do the right thing' to produce a formatted string result of a certain type."

One way you can approach this is create a Class:

using System;

namespace YourNameSpace
{
    public class DateWithCounter
    {
        public int ID { set; get; }
        public DateTime Date { set; get; }

        public DateWithCounter(DateTime theDate)
        {
            Date = theDate;
            ID = counter++;
        }

        // ToString override
        public override string ToString()
        {
            return string.Format("{0}{1}{2}{3}{4}", 
                Date.Year, 
                dash, 
                Date.Month.ToString().PadLeft(4,zero), 
                dash, 
                ID.ToString().PadLeft(4,zero));
        }

        // static elements

        private static char dash = '-';
        private static char zero = '0';
        private static int counter = 0;
    }
}

Okay, let's test the class:

private void SomeButton_Click(object sender, EventArgs e)
{
    List<DateWithCounter> dtCounters = new List<DateWithCounter>();

    DateTime adate;

    for (int i = 1; i < 13; i++)
    {
       adate = new DateTime(2015,i, i*2);
       dtCounters.Add(new DateWithCounter(adate));

       // test string output
       Console.WriteLine(dc);
    }
}

Here's the output:

2015-0001-0000
2015-0002-0001
2015-0003-0002
2015-0004-0003
2015-0005-0004
2015-0006-0005
2015-0007-0006
2015-0008-0007
2015-0009-0008
2015-0010-0009
2015-0011-0010
2015-0012-0011

I would hope that your take-away from this example is that C#/.NET offers you powerful tools to "design Objects/Types," as well as solve problems by clever coding.

I would also argue that developing the skill to design Types/Objects results in code that has a higher quality of "encapsulation."

However, I should disclaimer the above statement by saying that knowing when to take a particular coding problem and create a small utility/tool to use in that problem, and in other similar problems, is an equally valuable skill.

In this particular case, I don't see a need to create a tool/utility. Of course, it would be easy to create a generic class that had a static counter variable !

Quote:

Tell me and I forget. Teach me and I remember. Involve me and I learn.
Benjamin Franklin


No.
Dash is a string literal, and does not have numeric meaning, you need to customize the logic to accomplish your task
Split variable with - and then convert it to int and then do increment
see below snippet

string szValue = "2014-11-0001";
int iCount = convert.toInt32(szValue.Split('-')[2]);
iCount = iCount + 1;
int val= iCount.ToString("D").Length + 3;
szValue =  "2014-11-" + convert.toString(val);


in above logic you can check of value '9999' and increment another value

Hope it helps


You are taking the problem from the wrong end.
You have to answer the question "How is built the reference ?"
In your case, the reference is build from a year, a month and a counter.
Normal app are keeping track of the components of this reference and only build the reference on request.

If your app don't keep track of the components, you have to split the reference in its components, then generate the next reference.

This look like HomeWork, so start to work, and come back here when you have a problem in your code.


这篇关于使用Dash将字符串转换为数字的C#问题( - )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆