Manipulation of strings in the D language

Source: Internet
Author: User

The manipulation of strings is particularly important in software development, because basically programming uses strings, and string manipulation determines the good and bad of a language. There have been string manipulation performance issues in one of the projects I've worked on.

The D language string has string,wstring,dstring three types, and in the D language the strings are defined using a character array, and three types correspond to Char,wchar,dchar respectively. Char has only one byte, WCHAR is double-byte, and Dchar is three bytes. The operation of a string is also equivalent to an array of operations, which is not the same as other languages, the string in C + + is encapsulated in the string class, its operation is done on the function provided by the string class, but also in C #. The characters in D are more like strings in the C language.

The D language has made a great improvement in the array, which also makes the D-language string behave better in performance than string operations in other languages. Take a look at the first example:

Import Std.stdio; int Main (string[] argv) {    = "Hello honan!" ;     int n = 8;     = Tmp[0. $];     = tmp[0..5];     = Tmp[0. n];    Writeln (SUB1);    Writeln (SUB2);    Writeln (SUB3);    Readln ();     return 0;}

Run output
Look at the program above, auto sub1 = tmp[0..$], $ represents the maximum length of the array, which is to take the entire string.

This is a substring of the operation, so that the string is simple to write, and the operation of the array directly into the string operation, do not need a separate substring function to complete. The manipulation of strings at the compiler level is handled and optimized, which can also greatly improve the performance of string manipulation.

One might say that, in addition to the compiler might do some optimizations, but also improve how much performance? Haha, if you think so well, take a look at the following example:

Import Std.stdio; int Main (string[] argv) {    = "Hello honan!" ;     = tmp[0..5];    (CAST (char[]) (SUB2)) [3] = ' K ';    WRITELN (TMP);    Writeln (SUB2);    Readln ();     return 0;}

Run output

Look at this run result, see no, the original string and substring 3rd characters are K, this is a magical place, because it shows that sub2 this substring does not produce the actual memory copy. This will greatly improve the performance of the operation when it comes to string manipulation. This is a great improvement.

Crossing is seeing this sentence (cast (char[]) (SUB2)) [3] = ' K '; it may be said that the assignment of a character is so complicated. Haha, don't look at him first. Complex, this is very useful, this method is called strong-type language features, this can make you compile (rather than run the program) will find the problem, not easy to manipulate the contents of the string, because it is rarely directly modify the contents of the string. Most of the operations are done using third-party functions.

And someone else would think, if my substring needs to be copied, it's simple, using the DUP function to do it:

Import Std.stdio; int Main (string[] argv) {    = "Hello honan!" ;     = tmp[0..5].dup;    (CAST (char[]) (SUB2)) [3] = ' K ';    WRITELN (TMP);    Writeln (SUB2);    Readln ();     return 0;}

Auto Sub2 = tmp[0..5].dup; The DUP function in this sentence allows the string to be copied.

Let's take a look at the connection operation of the character:

Import Std.stdio; Import std.string; int Main (string[] argv) {    = "Hello honan!" ;     = tmp ~ "End";    WRITELN (TMP);    Writeln (TMP2);    Readln ();     return 0;}

Conveniently, using the ~ operator directly can be done.

These features are far from the full functionality of string manipulation, requiring more string manipulation capabilities to import the Std.string package, which includes functions in this package: Indexof,lastindexof,lastindexofany,representation, Capitalize,splitlines,strip,stripleft,stripright,

Chomp,chompprefix,chop,equal,leftjustify,rightjustify,center,detab,entab,translate,

FORMAT,SFORMAT,XFORMAT,XSFORMAT,INPATTERN,COUNTCHARS,REMOVECHARS,SQUEEZE,MUNCH,SUCC,TR,

Isnumeric,soundex,abbrev,column,wrap,outdent.

So many string operation, is really too rich, really cow B works ah, so much can not be introduced one by one, this function in the STRING.D has unittest routines, see can understand, here only do a few key functions introduced:

is a unit test in a STRING.D this is the power of unittest syntax, not only to test in the programming, but also to put the test code in the program together, so that later users know very clearly how the function is used, is really a cow B's invention. To look at the go language, it is really just a toy, to use the D language of the struct, let those who use go to understand D language in that aspect of performance is not bad. This is really worth learning language, can be simple, more in-depth.

Let's take a look at some important things about strings.

The Format function :

When working with strings, we often need to format the characters using the Format function.

~ ~ ~ Continue tomorrow

Manipulation of strings in the D language

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.