简体   繁体   English

在vb.net中格式化字符串

[英]Format strings in vb.net

Is there anything like formatting a string to the format specified? 是否有将string格式化为指定格式的方法?

Eg: 例如:

If the string value is 如果字符串值为

"05/05/2013 05:06:23"

If the formatting type is 如果格式化类型是

"d"

So if we pass both the values, the output should be 因此,如果我们同时传递两个值,则输出应为

05/05/2013

I tried like using the function Format("stirng value","format") , but this gave me the format as o/p. 我尝试使用函数Format("stirng value","format") ,但这给了我o / p的format

Note : The function should work for all the datatypes . 注意 :该函数应适用于所有datatypes

You'll have to parse it into a DateTime object and then use the ToString again with your specified format. 您必须将其解析为DateTime对象,然后以指定的格式再次使用ToString

Dim str = "05/05/2013 05:06:23"
Dim ci = new CultureInfo("en-US")
Dim formattedDate = DateTime.Parse(str).ToString("dd/MM/yyyy", ci) // Returns 05/05/2013"

And no, there is no such thing which works for all data types. 不,没有适用于所有数据类型的东西。

If you want to convert another data type ( Decimal for example ), first convert your string into the respective type ( Decimal.Parse ..etc) and then use ToString again for the desired format. 如果要转换其他数据类型(例如Decimal ),请首先将字符串转换为相应的类型( Decimal.Parse ..etc),然后再次将ToString用作所需的格式。

You can create your own function like this .. 您可以像这样创建自己的函数..

This function can accept any type and return any type (only tested on int and date) 此函数可以接受任何类型并返回任何类型(仅在int和date上进行了测试)

Function ChangeThis(ByVal var As Object, ByVal Type As String) As Object
    If Not var.GetType.IsValueType() Then Return Nothing
    Select Case var.GetType().ToString
        Case "System.Int32" : MsgBox("Integer 32")
        Case "System.DateTime"
            If Type = "d" Then Return Convert.ToDateTime(var).Date
    End Select
    Return ""
End Function

You can test it 你可以测试一下

Dim s As Date = ChangeThis(Now(), "d")
MsgBox(s)

This is not too good, but it can help .. 这不是很好,但是可以帮助..

The VB.NET Format function will give you what you want; VB.NET Format功能将为您提供所需的内容。 you just have to read the documentation to ensure you supply known formats: 您只需阅读文档以确保提供已知格式:

> (Strings:Format "12-03-04 5:01:23.456" "d")
d
> (Strings:Format "12-03-04 5:01:23.456" "Short Date")
12/03/2004
> (Strings:Format "12-03-04 5:01:23.456" "Long Date")
Friday, 12 March 2004
> (Strings:Format "50123.456" "Currency")
$50,123.46
>

The above is actually DotLisp and its output, but it is just calling Microsoft.VisualBasic.Strings.Format . 上面实际上是DotLisp及其输出,但它只是调用Microsoft.VisualBasic.Strings.Format

为此,您必须首先将字符串解析为datetime,然后可以使用:

myDateVariable.ToString("d")

Try using the .net string.format function. 尝试使用.net string.format函数。 Howver, the format string needs to specify exactly what output you want. 但是,格式字符串需要确切指定所需的输出。 Just using "format" as the format string will output "format", as you've already discovered. 正如您已经发现的,仅使用“ format”作为格式字符串将输出“ format”。

尝试

Convert.ToDateTime("05/05/2013 05:06:23").ToString("dd/MM/yyyy")

Your can try this. 您可以尝试一下。

 Dim strRawdate As String = "05/05/2013 05:06:23"
 Dim dt As DateTime = DateTime.ParseExact(strRawdate, "M/d/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
 Dim reformatDate As String = dt.ToString("M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture)

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

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