在java中转义变量

时间:2013-10-01 04:25:58

标签: java escaping

你可以在Java中转义变量吗?例如,在PHP中,我可以这样做:

$test = 3;
echo "He is {$test} years old";

将输出:

 He is 3 years old

你能用Java做类似的事吗?

3 个答案:

答案 0 :(得分:6)

您应该使用String.format

int age = 3;
String newString = String.format("He is %d years old", age);

%d - 说int。 %s - 用于字符串,%f用于float,以及越来越多的格式化选项。

你可以找到一个非常好的explenation here

诺姆。

答案 1 :(得分:2)

我认为你需要这种方式

Int x=3;
String test="He is "+x+"years old";
System.out.println(test);
output
He is 3 years old

答案 2 :(得分:0)

我可能误解了这个问题,但我会这样做

Public Sub acToxlRecordsets()

    Dim xlApp As Object, xlwkb As Object
    Dim db As Database
    Dim rst As Recordset
    Dim args As Collection, arg As Variant
    Dim strPath As String, strSQL As String
    Dim i As Integer
    Dim fld As Field

    ' INITIALIZE OBJECTS 
    Set db = CurrentDb()
    Set xlApp = CreateObject("Excel.Application")

    args.Add ("arg1")
    args.Add ("arg2")
    args.Add ("arg3")

    strPath = "C:\Path\To\Excel\Files"

    i = 1
    For Each arg In args
        ' CREATE NEW WORKBOOK
        Set xlwkb = xlApp.Workbooks.Open(strPath & "\ExcelFile" & i)

        ' OPEN NEW RECORDSET
        strSQL = "select * from myQuery where arguments = " & arg
        Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
        rst.MoveFirst

        ' OUTPUT RECORDSET
        ' FIRST COLUMNS
        xlwkb.Worksheets(1).Activate
        xlwkb.Worksheets(1).Range("A1").Select

        For Each fld In rst.Fields
            xlApp.ActiveCell = fld.Name
            xlApp.ActiveCell.Offset(0, 1).Select
        Next

        ' NEXT ROWS
        xlwkb.Worksheets(1).Range("A2").CopyFromRecordset rst
        xlwkb.Worksheets(1).Range("A1").Select

        ' SAVE AND CLOSE EXCEL WORKBOOK
        xlwkb.Close True

        i = i + 1
    Next arg

    ' UNINITIALIZE OBJECTS
    rst.Close
    Set xlwkb = Nothing
    Set xlApp = Nothing
    Set rst = Nothing
    Set db = Nothing

End Sub