Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want to join string and Integer value for ID.

VB
Dim DatePart As String = DateTime.Now.ToString("yyyy/MM/dd")
    Dim result As String
   DatePart = DatePart.Replace("/", "")



'So the final result like this

2012041100001
2012041100002
|
|
|
|
|
2012041100020
2012041100200

So help me plz :-)
Posted

C#
DateTime date = DateTime.Now;
int integer = 1;
string whatever = date.ToString("yyyyMMdd") + integer .ToString("d5");

Should do what you want, without the unecessary replace. The integer to string will go out of the format if it is longer than 5 digits, but you have this problem anyway.
 
Share this answer
 
Comments
Manoj Kumar Choubey 12-Apr-12 7:56am    
this is Correct way to generate id's ......
But No need to initialize interer variable You need to get last value from the Series ....
Keith Barrow 12-Apr-12 10:18am    
This is only demo code, not the full thing. The ToString(...) stuff is what is important here. At some point the int does need to be initialised, I just added it here so to help the code clarity (same goes for the date time initialisation), the above should run in a console app so the OP can see what is happening.
VB
DateTime date = DateTime.Now;
string whatever = date.ToString("yyyyMMdd") + iCount.ToString("d5");


iCount will Increment one by one ......
 
Share this answer
 
v2
Comments
Keith Barrow 12-Apr-12 9:14am    
I think you missed a ++:
string whatever = date.ToString("yyyyMMdd") + ++iCount.ToString("d5");
Manoj Kumar Choubey 12-Apr-12 9:34am    
yes but I am not using any loop
Above two Line's may be use in function and iCount will pass as parameter ,
and May be use in loop and put ++iCount.ToString("d5");
Keith Barrow 12-Apr-12 10:27am    
If the OP can't work out how to pass the int/datetime, then he/she has bigger problems, my guess is that he/she can. As you say we don't know the exact situation, but it is obvious that it is the formatting of the date and integer the OP is having problems with not what to pass in. You said "iCount will increment one by one", but in your code it won't. I assumed you wanted to increment it in your code, otherwise your solution is *identical* to mine for all practical purposes.
Manoj Kumar Choubey 12-Apr-12 9:35am    
We don't know program situation ...
Hi,
try this
VB
Dim str As String = Date.now.ToString("yyyy/MM/dd")
Dim sb As New System.Text.StringBuilder

For Each ch As Char In str
    If Char.IsLetterOrDigit(ch) OrElse ch = " "c Then
        sb.Append(ch)
    End If
Next

MessageBox.Show(sb.ToString())
 
Share this answer
 
Comments
Manoj Kumar Choubey 12-Apr-12 7:49am    
I think something is wrong in above code .....
Manoj Kumar Choubey 12-Apr-12 7:50am    
Why do you using String Builder ?
If He need this numbering Separately .... ?
Keith Barrow 12-Apr-12 9:11am    
StringBuilder, despite the conventional wisdom, isn't *always* efficient. It is particularly bad for short string concatenations (i.e. where only a few items are being concatenated), and concatenations where the number of elements to be added is known at compile-time (both these conditions are met here). This is because it takes time to instantiate the StringBuilder object and it allocates a reasonable chunk of memory when it initializes. Take a look at http://www.yoda.arachsys.com/csharp/stringbuilder.html
You just have to add :

C#
result = string.Format("{0}{1}", DatePart, i);


i being your integer value.

BTW : avoid concatenating strings using + ; better use string.Format() or string.Concat().
 
Share this answer
 
v2
Dim DatePart As String = DateTime.Now.ToString("yyyyMMddhhmmss")


Now result is "20120412092544"
2012= year
04 = month
12 = date
09 = hour
25 = minutes
44= second

I want to use the following format in my Invoice.
Every transaction will increment one by one.
Like that,

Bill NO: 201204120925440001
Bill NO: 201204120925440002
Bill NO: 201204120925440003
Bill NO: 201204120925440004
Bill NO: 201204120925440005
Bill NO: 201204120925440006
Bill NO: 201204120925440007
Bill NO: 201204120925440008
Bill NO: 201204120925440009
Bill NO: 201204120925440010

I think, this format will join String data type & Integer data type. So, I want to know how to join String and Integer.
thz for all answer!!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900