www.digitalmars.com Home | Search | D | Comments
Last update Mon Aug 22 2005
D
Language
Phobos
Comparisons


object

std
 std.base64
 std.boxer
 std.compiler
 std.conv
 std.ctype
 std.date
 std.file
 std.format
 std.gc
 std.intrinsic
 std.math
 std.md5
 std.mmfile
 std.openrj
 std.outbuffer
 std.path
 std.process
 std.random
 std.recls
 std.regexp
 std.socket
 std.socketstream
 std.stdint
 std.stdio
 std.cstream
 std.stream
 std.string
 std.system
 std.thread
 std.uri
 std.utf
 std.zip
 std.zlib

std.windows

std.linux

std.c
 std.c.stdio

std.c.windows

std.c.linux

std.string

To copy or not to copy?

When a function takes a string as a parameter, and returns a string, is that string the same as the input string, modified in place, or is it a modified copy of the input string? The D array convention is "copy-on-write". This means that if no modifications are done, the original string (or slices of it) can be returned. If any modifications are done, the returned string is a copy.

class StringException
Thrown on errors in string functions.

const char[] hexdigits;
"0123456789ABCDEF"

const char[] digits;
"0123456789"

const char[] octdigits;
"01234567"

const char[] lowercase;
"abcdefghijklmnopqrstuvwxyz"

const char[] uppercase;
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

const char[] letters;
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

const char[] whitespace;
" \t\v\r\n\f"

long atoi(char[] s)
Convert string to integer.

real atof(char[] s)
Convert string to real.

int cmp(char[] s1, char[] s2)
Compare two strings. Returns:
<0 for (s1 < s2)
=0 for (s1 == s2)
>0 for (s1 > s2)

int icmp(char[] s1, char[] s2)
Same as cmp() but case insensitive.

char* toStringz(char[] s)
Converts a D array of chars to a C-style 0 terminated string.

int find(char[] s, dchar c)
Find first occurrance of c in string s. Return index in s where it is found. Return -1 if not found.

int rfind(char[] s, dchar c)
Find last occurrance of c in string s. Return index in s where it is found. Return -1 if not found.

int find(char[] s, char[] sub)
Find first occurrance of sub in string s. Return index in s[] where it is found. Return -1 if not found.

int rfind(char[] s, char[] sub)
Find last occurrance of sub in string s. Return index in s where it is found. Return -1 if not found.

int ifind(char[] s, dchar c)
int irfind(char[] s, dchar c)
int ifind(char[] s, char[] sub)
int irfind(char[] s, char[] sub)
Case insensitive versions.

char[] tolower(char[] s)
Convert string to lower case.

char[] toupper(char[] s)
Convert string to upper case.

char[] capitalize(char[] s)
Capitalize first character of string.

char[] capwords(char[] s)
Capitalize all words in string. Remove leading and trailing whitespace. Replace all sequences of whitespace with a single space.

char[] join(char[][] words, char[] sep)
Concatenate all the strings together into one string; use sep[] as the separator.

char[][] split(char[] s)
Split s[] into an array of words, using whitespace as the delimiter.

char[][] split(char[] s, char[] delim)
Split s[] into an array of words, using delim[] as the delimiter.

char[][] splitlines(char[] s)
Split s[] into an array of lines, using CR, LF, or CR-LF as the delimiter.

char[] stripl(char[] s)
char[] stripr(char[] s)
char[] strip(char[] s)
Strips leading or trailing whitespace, or both.

char[] chomp(char[] s, char[] delimiter = null)
Returns s sans trailing delimiter, if any. If delimiter is null, any trailing CR, LF, or CRLF is removed.

char[] chop(char[] s)
Returns s sans trailing character, if there is one. If last two characters are CR-LF, then both are removed.

char[] ljustify(char[] s, int width)
char[] rjustify(char[] s, int width)
char[] center(char[] s, int width)
Left justify, right justify, or center string s in field width chars wide.

char[] zfill(char[] s, int width)
Same as rjustify(), but fill with '0's.

char[] replace(char[] s, char[] from, char[] to)
Replace occurrences of from with to in s.

char[] replaceSlice(char[] s, char[] slice, char[] replacement)
Given a string s with a slice into it, replace slice[] with replacement.

char[] insert(char[] s, int index, char[] sub)
Insert sub into s at location index.

int count(char[] s, char[] sub)
Count up all instances of sub in s.

char[] expandtabs(char[] s, int tabsize)
Replace tabs with the appropriate number of spaces. tabsize is the distance between tab stops.

char[] maketrans(char[] from, char[] to)
Construct translation table for translate().

char[] translate(char[] s, char[] transtab, char[] delchars)
Translate characters in s using table created by maketrans(). Delete chars in delchars. Note: This only works if s is ASCII. Use tr for full UCS character support.

char[] toString(bit arg)
char[] toString(char arg)
char[] toString(byte arg)
char[] toString(ubyte arg)
char[] toString(short arg)
char[] toString(ushort arg)
char[] toString(int arg)
char[] toString(uint arg)
char[] toString(long arg)
char[] toString(ulong arg)
char[] toString(float arg)
char[] toString(double arg)
char[] toString(real arg)
char[] toString(ifloat arg)
char[] toString(idouble arg)
char[] toString(ireal arg)
char[] toString(cfloat arg)
char[] toString(cdouble arg)
char[] toString(creal arg)
Convert arg to string.

char[] toString(long arg, uint radix)
Convert arg to string in radix radix. radix must be a value from 2 to 36. arg is treated as a signed value only if radix is 10. The characters A through Z are used to represent values 10 through 36.

char[] toString(ulong arg, uint radix)
Convert arg to string in radix radix. radix must be a value from 2 to 36. The characters A through Z are used to represent values 10 through 36.

char[] toString(char* s)
Convert C-style 0 terminated string s to char[] string.

char[] format(...)
Format arguments into a string.

char[] sformat(char[] s, ...)
Format arguments into string s which must be large enough to hold the result. Throws ArrayBoundsError if it is not. Returns s.

char[] succ(char[] s)
Return string that is the 'successor' to s. If the rightmost character is a-zA-Z0-9, it is incremented within its case or digits. If it generates a carry, the process is repeated with the one to its immediate left.
	succ(null);	// returns null	
	succ("!@#$%");	// returns "!@#$%"
	succ("1");	// returns "2"
	succ("9");	// returns "10"
	succ("999");	// returns "1000"
	succ("zz99");	// returns "aaa00"

char[] tr(char[] str, char[] from, char[] to, char[] modifiers = null)
Replaces characters in str that are in from with corresponding characters in to and returns the resulting string.

modifiers is a string of modifier characters:

Modifier Description
c Complement the list of characters in from
d Removes matching characters with no corresponding replacement in to
s Removes adjacent duplicates in the replaced characters

If modifier d is present, then the number of characters in to may be only 0 or 1.

If modifier d is not present and to is null, then to is taken to be the same as from.

If modifier d is not present and to is shorter than from, then to is extended by replicating the last character in to.

Both from and to may contain ranges using the - character, for example a-d is synonymous with abcd. Neither accept a leading ^ as meaning the complement of the string (use the c modifier for that).

Patterns

A pattern is an array of characters much like a character class in regular expressions. A sequence of characters can be given, such as "abcde". The '-' can represent a range of characters, as "a-e" represents the same pattern as "abcde". "a-fA-F0-9" represents all the hex characters. If the first character of a pattern is '^', then the pattern is negated, i.e. "^0-9" means any character except a digit. The following functions use patterns.

Note: In the future, the pattern syntax may be improved to be more like regular expression character classes.

int inPattern(dchar c, char[] pattern)
Returns 1 if c is in pattern, 0 if not.

int inPatterns(dchar c, char[][] patterns)
Returns 1 if c is in each of the patterns in patterns, 0 if not.

size_t countchars(char[] s, char[] pattern)
Returns number of characters in s that match pattern.

char[] removechars(char[] s, char[] pattern)
Return a string consisting of s with all the characters that match pattern removed.

char[] squeeze(char[] s, char[] pattern = null)
Return a string consisting of s with all the multiple sequences of characters that match pattern are removed. If pattern is null, it defaults to all characters.
	squeeze("hello goodbye")	// returns "helo godbye"
	squeeze("hello goodbye", "le")	// returns "helo goodbye"

Feedback and Comments

Add feedback and comments regarding this page.
Copyright © 1999-2005 by Digital Mars, All Rights Reserved