在vb.net中获取字符串的一部分 [英] Getting a part of the string in vb.net

查看:1079
本文介绍了在vb.net中获取字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串下面的数字存储到另一个字符串中.
" targetString应该为"3577610"

我的代码是:

Dim sourceString as string ="Cardrew Industrial.3577610(kWh)"
将targetString变暗为字符串= sourceString.Substring(sourceString.IndexOf(.")+ 1,sourceString.length-6)

但这会引发错误消息.

有任何建议吗?

I want to store the number from below string into another string.
''targetString should be "3577610"

My code is:

Dim sourceString as string="Cardrew Industrial.3577610 (kWh)"
Dim targetString as string = sourceString.Substring(sourceString.IndexOf(".") + 1, sourceString.length-6)

But it is throwing an error message.

Any suggestions please?

推荐答案

Dim sourceString As String = "Cardrew Industrial.3577610 (kWh)"
Dim Index As Integer = sourceString.IndexOf(".") + 1
Dim IndexEnd As Integer = sourceString.LastIndexOf(" ")
Dim Length As Integer = sourceString.Length - IndexEnd
Dim targetString As String = sourceString.Substring(Index, Length)



第二个参数是所需字符串中的字符数,而不是字符串的结束位置.



The second argument is number of characters in the string that you want, not the end position of the string.


您误算了string.Substring的第二个参数.从逻辑上讲,它不是位置,而是长度.应该是:

You miscalculated the second parameter of string.Substring. It is, logically, not position but length. Should be:

Dim targetString as string = 
   sourceString.Substring(sourceString.IndexOf(".") + 1, 7)



但是,整个方法是错误的.使用6或7这样的立即常量(硬编码)将不会带您到任何地方.通常,您应该避免使用任何立即常量.它们必须作为配置文件中存储的一组显式常量,资源或变量进入.如果您进行硬编码,那么如何支持此编码?但是在这种情况下,长度,位置之类的任何常量都没有意义.

您可以将正则表达式与"\.[0-9] *"或"[0-9] *"等模式结合使用以提取匹配项.请参见System.Text.RegularExpressions.Regex http://msdn.microsoft.com/en-us /library/system.text.regularexpressions.regex.aspx [ ^ ].

即使您做得正确,这也是错误的做法.您不应该避免将数字存储在字符串中.尝试做相反的事情.仅当需要在UI中显示数据时,才使用数字类型并从中生成字符串.使用System.String.Format http://msdn.microsoft.com/en-us/library/system .string.aspx [^ ].好吧,如果可能的话.

—SA



However, the whole approach is wrong. Using immediate constants (hard-coded) like 6 or 7 will not lead you anywhere. You should generally avoid any immediate constants. They have to go to as set of explicit constants, resources or variables stored in configuration file(s). If you hard-code, how can you support this code? But in this case, any constants for length, position and the like makes no sense.

You could use Regular Expressions with the pattern like "\.[0-9]*" or "[0-9]*" to extract the match. See System.Text.RegularExpressions.Regex, http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx[^].

By even if you did it properly, it would be wrong approach. You should not avoid storing numeric in string. Try to do just the opposite. Work with numeric types and generate strings out of it only when you need to present data in UI. Use System.String.Format, http://msdn.microsoft.com/en-us/library/system.string.aspx[^]. Well, if possible.

—SA


如果这是一种模式,每次您想读取数字值,那么也可以使用正则表达式.
If it is a pattern that every time you want to read number value then you can use regular expression as well.


这篇关于在vb.net中获取字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆