简体   繁体   English

从Java到VB.net的String.split()和replaceAll

[英]String.split() and replaceAll from Java to VB.net

I have several lines of code in Java that i need to convert VB.net: 我有几行Java代码需要转换VB.net:

1) s.replaceAll("f\\((.*)\\).*", "$1").trim()
2) s.split("\\,")
3) s.split("\\s+")
4) s.replaceAll("[Tt]hreshold\\s*=(.*)", "$1").trim()

I used Java to VB.net converter and here is the result: 我使用Java到VB.net转换器,结果如下:

1) s = s.ReplaceAll("f\((.*)\).*", "$1").Trim()
2) s.Split("\,", True)
3) s.Split("\s+", True)
4) s.ReplaceAll("[Tt]hreshold\s*=(.*)", "$1").Trim()

The problem i have is that this code does not work. 我的问题是此代码无法正常工作。 Lines 2 and 3 have an error "Argument matching parameter 'separator' narrows from 'String' to '1 - Dimensional array of Char'". 第2行和第3行出现错误“参数匹配参数'separator'从'String'缩小为'1- Char的维数数组'”。 Lines 1 and 4 say that 'ReplaceAll' is not a member of 'String'. 第1行和第4行说'ReplaceAll'不是'String'的成员。 I am a bit puzzled, how could I do make it work the exact same way as Java code? 我有些困惑,如何使它与Java代码完全一样地工作? Help is very much appreciated. 非常感谢您的帮助。

In Java, unlike .NET, methods on String allow regular expressions. 在Java中,与.NET不同, String方法允许使用正则表达式。 In .NET, the Regular Expressions library (located in System.Text.RegularExpressions ) would be used instead. 在.NET中,将改用正则表达式库(位于System.Text.RegularExpressions )。 The converter is failing to identify this difference. 转换器无法识别此差异。

In both Java and .NET, strings are immutable, that is, the Java code does not change the value of s , and the lines by itself are no-ops. 在Java和.NET中,字符串都是不可变的,也就是说,Java代码不会更改s的值,而代码行本身是no-ops。 You would need to capture the result of each call, for instance as a variable, for later use. 您需要捕获每个调用的结果,例如作为变量,以备后用。

In the first line: 在第一行:

s.replaceAll("f\\((.*)\\).*", "$1").trim()

This can be expressed using the Regex library as 可以使用Regex库将其表示为

Regex.Replace(s, "f\((.*)\).*", "$1").Trim()

Now take the second line: 现在走第二行:

s.split("\\,")

This expression simply splits a comma-delimited string into an array. 该表达式只是将以逗号分隔的字符串拆分为一个数组。 (The comma does not need to be escaped; this expression is the same as s.split(",") ). (逗号无需转义;此表达式与s.split(",") )。 The equivalent VB code does not require regular expressions and is simply: 等效的VB代码不需要正则表达式,并且很简单:

s.split(","c)

(or, assigned to a variable, dim Sa as String() = s.Split(","c) .) (或分配给变量的dim Sa as String() = s.Split(","c) 。)


The third line: 第三行:

s.split("\\s+")

This pattern uses a regular expression to use a string delimited on whitespace (any number of characters). 此模式使用正则表达式来使用在空格(任何数量的字符)上定界的字符串。 The Regex.Split method returns the same result: Regex.Split方法返回相同的结果:

Regex.Split(s, "\s+")

Finally, take: 最后,采取:

s.replaceAll("[Tt]hreshold\\s*=(.*)", "$1").trim()

This construct is functionally identical to the first, albeit with a different pattern. 该构造与第一个构造在功能上相同,尽管具有不同的模式。 It can be expressed in VB as: 可以在VB中表示为:

Regex.Replace(s, "[Tt]hreshold\\s*=(.*)", "$1").Trim()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM