可以在等式中使用函数

时间:2017-05-26 13:57:51

标签: powershell

在Powershell中使用方程时,有没有办法调用函数。

我正在尝试类似以下的内容,但最后一行会返回错误You must provide a value expression...

function quote($str) {
    return """" + $str  + """";
};
$a= "abc: " + quote('hi'); # <-- Doesn't Work

我意识到我可以将引用分配给一个中间变量然后进行连接($q=quote('hi'); $a="abc: " + q$)但是我希望有一个更简单的语法。

2 个答案:

答案 0 :(得分:2)

这就是你的意思:

function quote($str) {
    return """" + $str  + """";
};
$a= "abc: " + $(quote('hi'));

# edit: as per Joey's comment, this will also work:
$a= "abc: " + (quote('hi'));

修改

使用PowerShell语法重写:

# Function name is verb-noun, from approved verbs
# https://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx

Function Add-Quote{

    # parameters this function takes
    param([string]$str)

    # No need for return and semi colon. 
    # I tend to use return as it makes my code reading easier 
    """" + $str  + """"
}

$a = "abc: " + (Add-Quote -str 'hi')

答案 1 :(得分:1)

您可以使用格式运算符View#onAttachedToWindow来插入字符串。

ViewGroup parent = (ViewGroup) getParent();
View target;
while ((target = parent.findViewById(id)) == null) {
    parent = (ViewGroup) parent.getParent();
}