2 Replies - 1163 Views - Last Post: 07 March 2013 - 07:22 AM Rate Topic: -----

#1 inkey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-June 12

Increment a String with numbers and letters

Posted 07 March 2013 - 03:07 AM

I have a string which i need to increment by 1.
The string layout i have is as follows "MD00494"
How would i increment this to "MD00496"
If it was a normal string witn numbers id parse it to a int. But with letters how do i do this.
Is This A Good Question/Topic? 0
  • +

Replies To: Increment a String with numbers and letters

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Increment a String with numbers and letters

Posted 07 March 2013 - 05:02 AM

.SubString the last 5 letters and the first two to get the id and the number by themselves. Then you can int.Parse the number, increment it, then add it back to the id to get your result.
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Increment a String with numbers and letters

Posted 07 March 2013 - 07:22 AM

I find that if you trying to increment strings, it is a sign of a bad design.

A better way is to create a class that encapsulate that functionality. You then have the option to override the .ToString function method, to return the full ID string.
Spoiler

This post has been edited by AdamSpeight2008: 07 March 2013 - 07:25 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1