开发者

String to Int for new patient ID

I need to convert a string in the database of patients to a int to create a new patient ID.

In the Hospital database, the pa开发者_运维技巧tient ID is a STRING, not an integer. It is of the form p99. To create a new ID, I need to take out the p, convert to an integer, add 1, then put a 0 in if the value is less than 10, then add back the p.

I am using Microsoft visual studio and C#. How would I go about this? Any help would be greatly appreciated!


You can use string.Substring Method and Int32.TryParse method.

String.Substring Method (Int32)

Retrieves a substring from this instance. The substring starts at a specified character position.

Int32.TryParse Method (String, Int32)

Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.

string patientId = "p99";
int id;
if (Int32.TryParse(patientId.Substring(1), out id))
{
    patientId = string.Format("p{0}{1}", id < 10 ? "0" : string.Empty, id);
    MessageBox.Show("Patient Id : " + patientId);
}
else
{
   MessageBox.Show("Error while retrieving the patient id.");
}


You can use int.Parse() (MSDN) to convert a string to an integer.


You can write a simple routine to do this.

Assuming there is always a leading 'p' you can just do sID = sID.substring(1) to remove the first character. Then you can use Int ID = Int16.Parse(sID) to convert to an Int (16-bit in this case). If you need 32-bit, use Int32.Parse

then ID++ or ID = ID+1 to increment by one.

Next, you need to convert back to a string with sID = ID.ToString()

Finally, do some string manipulation to test the length, add the leading '0' if length = 1, and the leading 'p'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜