gpt4 book ai didi

c# - 知道字符串是长字符串还是 Guid 的最佳方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:11 24 4
gpt4 key购买 nike

我在文本文件中有一些字符串表示对象 ID,有时 ID 很长,其他是 Guid,我需要解析它们。

快速的答案很明显,例如尝试解析一个 Guid,如果失败,尝试解析一个 long,但是。有什么好的方法(优雅的解决方案)可以知道某个字符串是 long 还是 Guid?

  ee02b525-9755-4a07-a46d-37bb5b060fd2 (Guid)
-48765 (long)

提前致谢。

最佳答案

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?

没有。 long.TryParseGuid.TryParse是要走的路。另一种可能性是测试字符串是否包含 - 但不在开头:

someString.IndexOf("-") > 1

这强烈表明该字符串不是 Int64。它可能是一个向导。但它可以是任何其他东西。当然,如果您知道此文件中只能有 Guids 和 longs,这可能是一个强有力的指示。

关于c# - 知道字符串是长字符串还是 Guid 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9112782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com