我想检查特定字符串中是否存在两个括号()
。开始和结束括号。
Dim example1 = "1x : (HxBxT) 80x120x44 cm"
Dim example2 = "1x : ( 80x120x44 cm"
在这种情况下,example1包含两个括号:(并且关闭一个),因此它传递了大小写。
在这种情况下,我想在这种情况下(HxBxT)
取整个值,然后用某些东西替换它(我会这样做)。之后我想再次插入替换,例如在最后我会得到这个:
Dim example1 = "1x : (whatever i put here) 80x120x44 cm"
答案 0 :(得分:3)
您可以使用正则表达式:
$array = [
[
'date' => '2017-12-20',
'name' => 'test A',
'kwa' => 'kwa1, kwa2'
],
[
'date' => '2017-12-20',
'name' => 'test B',
'kwa' => ''
],
[
'date' => '2017-12-21',
'name' => 'test C',
'kwa' => 'kwa1'
],
[
'date' => '2017-12-22',
'name' => 'test D',
'kwa' => ''
],
[
'date' => '2017-12-22',
'name' => 'test E',
'kwa' => ''
],
];
$formatted = [];
foreach ($array as $k => $v) {
$formatted[$v['date']] = $formatted[$v['date']] ?? [];
array_push($formatted[$v['date']], $k);
}
print_r($formatted);
如果你只是想知道是否有开口和关闭括号,你可以使用正则表达式:
example1 = Regex.Replace(example1, "\(.*\)", "(whatever i put here)")
或VB.NET Like
operator:
Dim containsBrackets = Regex.IsMatch(example1, "\(.*\)")
或Dim containsBrackets = example1 like "*(*)*"
:
String.IndexOf
这也使您可以使用Dim indexOfOpeningBracket = example1.IndexOf("(")
Dim indexOfClosingBracket = example1.IndexOf(")", indexOfOpeningBracket + 1)
Dim containsBrackets = indexOfOpeningBracket >= 0 AndAlso indexOfClosingBracket > 0
:
Substring
答案 1 :(得分:0)
你可以像这样
获取indexOf方法的帮助Dim openBracket = myText.IndexOf("(")
Dim closeBracket = myText.IndexOf(")")
如果openBracket和closeBracket中的任何一个是-1(意思是未找到),如果两者都是0或> 0,那么你的情况就会失败,这意味着你传递了案例
然后您可以使用这样的subString方法来获取字符串
Dim lenStr = openBracket + closeBracket + 1
dim repStr = myText.Substring(startIndex, lenStr)
dim newString = myText.Replace(repStr,"Whatever text you want to put here)"
我无法访问VB.net,所以我的语法可能有点不对,但这会给你一个想法
答案 2 :(得分:0)
如果您不想使用正则表达式,可以试试这个:
Dim str As String = "1x : (HxBxT) 80x120x44 cm"
Dim newValue As String = "whatever i put here"
If str Like "*(*)*" Then
str = String.Format("{0}{1}{2}",
Strings.Left(str, str.IndexOf("("c) + 1),
newValue,
Strings.Right(str, str.Length - str.IndexOf(")"c)))
End If
答案 3 :(得分:0)
在Regex方法中使用正则表达式。
Dim input As String = "x : (HxBxT) 80x120x44 cm"
Dim pattern As String = "\(.*\)"
Dim replacement As String = "(whatever i put here)"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)