473,378 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Adding index to string

Hi,
I need to add an index to "GC-TI-0001" so that i can form the following
strings "GC-TI-0002", "GC-TI-0003", "GC-TI-0004", etc. Is there a fast and
easy way to do this in .NET or do i need to parse and then add and then add
the zeros?
--
Thank you,
Ibrahim
Nov 3 '07 #1
4 1317
Ibrahim wrote:
Hi,
I need to add an index to "GC-TI-0001" so that i can form the following
strings "GC-TI-0002", "GC-TI-0003", "GC-TI-0004", etc. Is there a fast and
easy way to do this in .NET or do i need to parse and then add and then add
the zeros?
Parse, add and format is what you have to do.

int num;
if (str.Length == 10 && int.TryParse(str.Substring(6), out num)) {
str = str.Substring(0, 6) + (++num).ToString("0000");
} else {
// wrong string format
}

--
Göran Andersson
_____
http://www.guffa.com
Nov 3 '07 #2
Thanks for your reply!!!

Eversince i did this post, i came up with this, which is worked so far:

' to summarize
numberRepOfString = 5
newIndex = numberRepOfString + index
newString = String.Format("0005", newIndex)

but i don't know how it will do when the number is 0099 and then is
incremented by 1.
--
Thank you,
Ibrahim
"Göran Andersson" wrote:
Ibrahim wrote:
Hi,
I need to add an index to "GC-TI-0001" so that i can form the following
strings "GC-TI-0002", "GC-TI-0003", "GC-TI-0004", etc. Is there a fast and
easy way to do this in .NET or do i need to parse and then add and then add
the zeros?

Parse, add and format is what you have to do.

int num;
if (str.Length == 10 && int.TryParse(str.Substring(6), out num)) {
str = str.Substring(0, 6) + (++num).ToString("0000");
} else {
// wrong string format
}

--
Göran Andersson
_____
http://www.guffa.com
Nov 3 '07 #3
Ibrahim wrote:
Thanks for your reply!!!

Eversince i did this post, i came up with this, which is worked so far:

' to summarize
numberRepOfString = 5
newIndex = numberRepOfString + index
newString = String.Format("0005", newIndex)

but i don't know how it will do when the number is 0099 and then is
incremented by 1.
Hm... What is that code supposed to do, really? What does "Rep" in
"numberRepOfString" stand for? Repeat? Represent? Report? Where does the
number five come from, and what does the index variable contain?

If you want to format the number as three digits followed by a "5", it's
more clear to use a format containing it as a literal string: "000'5'".

--
Göran Andersson
_____
http://www.guffa.com
Nov 3 '07 #4
I like the PadLeft & PadRight methods on the string class. You can provide
a total width and what the padding character is (like '0'). It will then
pad the string with padding characters until it reaches the specified
length.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com
"Ibrahim" <Ib*****@discussions.microsoft.comwrote in message
news:91**********************************@microsof t.com...
Thanks for your reply!!!

Eversince i did this post, i came up with this, which is worked so far:

' to summarize
numberRepOfString = 5
newIndex = numberRepOfString + index
newString = String.Format("0005", newIndex)

but i don't know how it will do when the number is 0099 and then is
incremented by 1.
--
Thank you,
Ibrahim
"Göran Andersson" wrote:
>Ibrahim wrote:
Hi,
I need to add an index to "GC-TI-0001" so that i can form the
following
strings "GC-TI-0002", "GC-TI-0003", "GC-TI-0004", etc. Is there a fast
and
easy way to do this in .NET or do i need to parse and then add and
then add
the zeros?

Parse, add and format is what you have to do.

int num;
if (str.Length == 10 && int.TryParse(str.Substring(6), out num)) {
str = str.Substring(0, 6) + (++num).ToString("0000");
} else {
// wrong string format
}

--
Göran Andersson
_____
http://www.guffa.com
Nov 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrés Giraldo | last post by:
Hi! I'm adding an asp button to a datagrid on the ItemDataBound event, when the user clicks on this button, I basically remove the button and create other 2 buttons... my problem is.. the 2 last...
15
by: Stormkid | last post by:
Hey Gang, I'm trying to figure out the best way to add two times together of the format hh:mm:ss any suggestions would be great thanks Todd
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: TomislaW | last post by:
Is it possible to add query string on requested page e.a. i have request for index.aspx, but i need index.aspx?language=en is this possible tomislaw
0
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to...
2
by: Joe | last post by:
Anyone can suggest the best method of reading XML and adding data to ListView? Here is the xml data structure:: <xml> <site> <url>http://www.yahoo.com</url> <lastupdate></lastupdate>...
9
by: Neo Geshel | last post by:
I have strip-mined, strip-searched, and completely exhausted the Internet (up to the 30th page on Google, with 100 results per page!!), all without finding an answer to my question AS TO WHY IT...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
5
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
0
by: porky008 | last post by:
I need to add a search button to this and have no clue how to do it. Can some one please help me on this? import java.awt.*; import java.awt.event.*; import javax.swing.*; import...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.