时间:2019-03-17 标签:c#substringindexof
我有一个看起来像这样的字符串 - "FirstName||Sam LastName||Jones Address||123 Main ST ..."
(还有 100 个不同的值)
我只想从整个字符串中查找 Sam 和 Jones。
so string firstname = originalstring.substring
... 等等。
有谁知道我该怎么做?
添加 - 我想我忘了提及几件事。
FirstName||Sam\r\n MiddleName||\r\n LastName||Jones\r\n ....
因此,现在如果我计算对我没有帮助的字符数,则可能需要更多项目,而不仅仅是名字和姓氏。
i have a string which looks like this -"FirstName||Sam LastName||Jones Address||123 Main ST ..."
(100 more different values)
I want to find only Sam and Jones from the entire string.
so string firstname = originalstring.substring
... etc.
Does anyone know how I can do this?
ADDITION -
I think i forgot to mention couple of things.
FirstName||Sam\r\n MiddleName||\r\n LastName||Jones\r\n ....
So now if i count the number of characters that wont help me, cause could need more items other than just firstname and lastname.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用正则表达式:
请参阅此处的演示。
Use Regular expressions:
See its Demo here.
我认为这可能比 .Split 方法更好。如果你有 ||在“Sam”和“LastName”之间,那么您肯定想要 .Split。事实上,这可能会更好。
I think this might work better than the .Split approach. If you had || between 'Sam' and 'LastName' then you'd certainly want to .Split. As it is, this might be better.
我会将字符串拆分两次,一次在“”上,然后在 || 上再次拆分。获取名字和姓氏的值
I would split the string twice once on " " and then again on || to get the values of first and last name
像这样的
string firstname = originalstring.substring(indexof("FirstName") + 11, ((indexof("LastName) - indexof("FirstName") + 11 )
这样你就可以到达第一个|| 之后的字母,直到“姓氏”之前的第一个字母,姓氏也是如此,您只需将名字与姓氏和姓氏与地址
编辑:我的错...它不是
substring.(indexOf...
但它是originalString.Substring(origianlString.IndexOf("FirstName) + 11, (originalString.IndexOf("LastName") - originalString.IndexOf("FirstName") + 11)
并且在查找最后一个时name 不是 + 11 而是 10,因为"LastName".Length + + "||".Length = 10
something like this
string firstname = originalstring.substring(indexof("FirstName") + 11, ((indexof("LastName) - indexof("FirstName") + 11 )
this way you get to the first letter after || and till the first letter before "lastname" the same goes for surname you just switch firstname with lastname and lastname with adress
edit: my fault... it's not
substring.(indexOf...
but it'soriginalString.Substring(origianlString.IndexOf("FirstName) + 11, (originalString.IndexOf("LastName") - originalString.IndexOf("FirstName") + 11)
and when looking for last name it's not + 11 but 10 because"LastName".Length + + "||".Length = 10