I have some strings in a text files representing object ids, sometimes the id is a long and other is a Guid, and I need to parse them.
The quick answer is obvious, for example trying to parse a Guid and it if fails, try to parse a long, but. Is there any good way ( elegant solution ) to know if a certain string is a long or a Guid?
ee02b525-9755-4a07-a46d-37bb5b060fd2 (Guid)
-48765 (long)
Thanks in advance.
The quick answer is obvious, for example trying to parse a Guid and it if fails, try to parse a long, but. Is there any good way (elegant solution) to know if a certain string is a long or a Guid?
No. long.TryParse and Guid.TryParse is the way to go. Another possibility is to test if the string contains -
but not in the beginning:
someString.IndexOf("-") > 1
This is a strong indication that this string is not an Int64. It could be a Guid. But it could be anything else. Of course if you know that there can only be Guids and longs in this file it could be a strong indication.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.