Review of Common conversion methods to string

Source: Internet
Author: User
Code

Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Text. regularexpressions;

Namespace Stringtest
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
// Format output
Console. writeline (datetime. Now. tostring ( " Yyyy-mm-dd " ));
Console. writeline ( String . Format ( " {0} arrived in Beijing on {1: yyyy mm dd. " , " John " , Datetime. Now ));
Double D =   123.456789 ;
Console. writeline (D. tostring ( " C3 " ));

String S =   " 1, 2, 3, 4, 5, 6 " ;
String [] S2 = S. Split ( ' , ' ); // Note''
Foreach ( String I In S2)
{
Console. writeline (I );
}

//Specify a symbolic connection
StringJoin= String. Join ("-", S2 );
Console. writeline (join );

// Comparison of string and stringbuilder Efficiency
Datetime before = Datetime. now;
String A =   " Aaa " ;
For ( Int I =   0 ; I <   20000 ; I ++ )
{
A + = I;
}
Console. writeline ( " The time spent in string addition loop is {0} " , Datetime. Now - Before );

Datetime start = Datetime. now;
Stringbuilder sb =   New Stringbuilder ( " Aaa " );
For ( Int I =   0 ; I <   20000 ; I ++ )
{
SB. append (I );
}
Console. writeline ( " Stringbuilder's aggregate cycle takes {0} " , Datetime. Now - Start );

// Type conversion
String St =   " 123e " ;
Int Dd;
If ( Int . Tryparse (St, Out Dd ))
{
Console. writeline (dd );
}
Else
{
Console. writeline ( " Enter the correct number! " );
}

// Determines whether the string is null.
String Em =   "" ;
If ( String . Isnullorempty (EM ))
{
Console. writeline ( " Em is an empty string! " );
}

// Intercept
String Tr =   " --- BCD --- " ;
String End = Tr. Trim ( ' - ' );
Console. writeline ( String . Format ( " {0}: Length: {1} " , End, end. Length ));

// Toupper () has been optimized to improve efficiency.
String YZ =   " ABC " ;
String SZ =   " ABC " ;
Console. writeline (YZ. toupper () = SZ. toupper ());
// Recommended MVP
Console. writeline ( String . Compare (YZ, SZ, True ) =   0 );

// Regular Expression
RegEx re =   New RegEx ( " [A-Za-Z] + " , Regexoptions. None );
String [] Lines = Re. Split ( " 12r4t " );
Foreach ( String Line In Lines)
{
Console. writeline (line );
}

Matchcollection MC = Re. Matches ( " T45yu " );
Foreach (Match Ma In MC)
{
Console. writeline (MA );
}
Console. readkey ();
}
}
}

 

Let's review what others have summarized.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.