Movatterモバイル変換


[0]ホーム

URL:


www.digitalmars.com

D Programming Language 1.0

Last update Mon Dec 31 10:53:28 2012

std

std.string

String handling functions.

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

Source:
std/string.d

classStringException: object.Exception;
Thrown on errors in string functions.

const char[16u]hexdigits;
0..9A..F

const char[10u]digits;
0..9

const char[8u]octdigits;
0..7

const char[26u]lowercase;
a..z

const char[26u]uppercase;
A..Z

const char[52u]letters;
A..Za..z

const char[6u]whitespace;
ASCIIwhitespace

const dcharLS;
UTF line separator

const dcharPS;
UTF paragraph separator

const char[2u]newline;
Newline sequence for this system

booliswhite(dcharc);
Returnstrue ifc is whitespace

longatoi(char[]s);
Convert string to integer.

realatof(char[]s);
Convert string to real.

intcmp(char[]s1, char[]s2);
inticmp(char[]s1, char[]s2);
Compare two strings.cmp is case sensitive, icmp is case insensitive.

Returns:
< 0s1 <s2
= 0s1 ==s2
> 0s1 >s2


char*toStringz(char[]s);
Convert array of charss[] to a C-style 0 terminated string.s[] must not contain embedded 0's.

ptrdiff_tfind(char[]s, dcharc);
ptrdiff_tifind(char[]s, dcharc);
ptrdiff_trfind(char[]s, dcharc);
ptrdiff_tirfind(char[]s, dcharc);
find, ifind find first occurrence ofc in strings. rfind, irfind find last occurrence ofc in strings.

find, rfind are case sensitive; ifind, irfind are case insensitive.

Returns:
Index ins wherec is found, -1 if not found.

ptrdiff_tfind(char[]s, char[]sub);
ptrdiff_tifind(char[]s, char[]sub);
ptrdiff_trfind(char[]s, char[]sub);
ptrdiff_tirfind(char[]s, char[]sub);
find, ifind find first occurrence ofsub[] in strings[]. rfind, irfind find last occurrence ofsub[] in strings[].

find, rfind are case sensitive; ifind, irfind are case insensitive.

Returns:
Index ins where c is found, -1 if not found.

stringtolower(strings);
Convert strings[] to lower case.

stringtoupper(strings);
Convert strings[] to upper case.

char[]capitalize(char[]s);
Capitalize first character of strings[], convert rest of strings[] to lower case.

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

char[]repeat(char[]s, size_tn);
Return a string that consists ofs[] repeatedn times.

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

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

char[][]split(char[]s, char[]delim);
Splits[] into an array of words, usingdelim[] as the delimiter.

char[][]splitlines(char[]s);
Splits[] into an array of lines, using CR, LF, or CR-LF as the delimiter. The delimiter is not included in the line.

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);
Returnss[] sans trailingdelimiter[], if any. Ifdelimiter[] isnull, removes trailing CR, LF, or CRLF, if any.

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

char[]ljustify(char[]s, intwidth);
char[]rjustify(char[]s, intwidth);
char[]center(char[]s, intwidth);
Left justify, right justify, or center strings[] in fieldwidth chars wide.

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

char[]replace(char[]s, char[]from, char[]to);
Replace occurrences offrom[] withto[] ins[].

char[]replaceSlice(char[]string, char[]slice, char[]replacement);
Return a string that isstring[] withslice[] replaced byreplacement[].

char[]insert(char[]s, size_tindex, char[]sub);
Insertsub[] intos[] at locationindex.

size_tcount(char[]s, char[]sub);
Count up all instances ofsub[] ins[].

char[]expandtabs(char[]string, inttabsize = 8);
Replace tabs with the appropriate number of spaces.tabsize is the distance between tab stops.

char[]entab(char[]string, inttabsize = 8);
Replace spaces instring with the optimal number of tabs. Trailing spaces or tabs in a line are removed.

Params:
char[]stringString to convert.
inttabsizeTab columns aretabsize spaces apart.tabsize defaults to 8.

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

BUG:
only works with ASCII

char[]translate(char[]s, char[]transtab, char[]delchars);
Translate characters ins[] using table created by maketrans(). Delete chars indelchars[].

BUG:
only works with ASCII

char[]toString(boolb);
char[]toString(charc);
char[]toString(ubyteub);
char[]toString(ushortus);
char[]toString(uintu);
char[]toString(ulongu);
char[]toString(byteb);
char[]toString(shorts);
char[]toString(inti);
char[]toString(longi);
char[]toString(floatf);
char[]toString(doubled);
char[]toString(realr);
char[]toString(ifloatf);
char[]toString(idoubled);
char[]toString(irealr);
char[]toString(cfloatf);
char[]toString(cdoubled);
char[]toString(crealr);
Convert to char[].

char[]toString(longvalue, uintradix);
char[]toString(ulongvalue, uintradix);
Convertvalue to string in radixradix.

radix must be avalue from 2 to 36.value is treated as a signedvalue only ifradix is 10. The characters A through Z are used to represent values 10 through 36.

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

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

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

Returns:
s

boolinPattern(dcharc, char[]pattern);
See if characterc is in thepattern.

Patterns:
Apattern is an array of characters much like acharacter 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 samepattern as "abcde". "a-fA-F0-9" represents all the hex characters. If the first character of apattern is '^', then thepattern is negated, i.e. "^0-9" means any character except a digit. The functionsinPattern,countchars,removeschars, andsqueeze use patterns.

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

intinPattern(dcharc, char[][]patterns);
See if characterc is in the intersection of thepatterns.

size_tcountchars(char[]s, char[]pattern);
Count characters ins that matchpattern.

char[]removechars(char[]s, char[]pattern);
Return string that iss with all characters removed that matchpattern.

char[]squeeze(char[]s, char[]pattern = null);
Return string where sequences of a character ins[] frompattern[] are replaced with a single instance of that character. Ifpattern isnull, it defaults to all characters.

char[]succ(char[]s);
Return string that is the 'successor' tos[]. 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.

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

Params:
char[]modifiersa string of modifier characters

Modifiers:
ModifierDescription
cComplement the list of characters infrom[]
dRemoves matching characters with no corresponding replacement into[]
sRemoves adjacent duplicates in the replaced characters


If modifierd is present, then the number of characters into[] may be only 0 or 1.

If modifierd is not present andto[] isnull, thento[] is taken to be the same asfrom[].

If modifierd is not present andto[] is shorter thanfrom[], thento[] is extended by replicating the last character into[].

Bothfrom[] andto[] may contain ranges using the- character, for examplea-d is synonymous withabcd. Neither accept a leading^ as meaning the complement of the string (use thec modifier for that).

final boolisNumeric(char[]s, boolbAllowSep = false);
[in] char[]s can be formatted in the following ways:

Integer Whole Number: (for byte, ubyte, short, ushort, int, uint, long, and ulong) ['+'|'-']digit(s)[U|L|UL]

Examples:
123, 123UL, 123L, +123U, -123L

Floating-Point Number: (for float, double, real, ifloat, idouble, and ireal) ['+'|'-']digit(s)[.][digit(s)][[e-|e+]digit(s)][i|f|L|Li|fi]] or [nan|nani|inf|-inf]

Examples:
+123., -123.01, 123.3e-10f, 123.3e-10fi, 123.3e-10L

(for cfloat, cdouble, and creal) ['+'|'-']digit(s)[.][digit(s)][[e-|e+]digit(s)][+] [digit(s)[.][digit(s)][[e-|e+]digit(s)][i|f|L|Li|fi]] or [nan|nani|nan+nani|inf|-inf]

Examples:
nan, -123e-1+456.9e-10Li, +123e+10+456i, 123+456

[in] boolbAllowSep False by default, but when set totrue it will accept the separator characters "," and "" within the string, but these characters should be stripped from the string before using any of the conversion functions like toInt(), toFloat(), and etc else an error will occur.

Also please note, that no spaces are allowed within the string anywhere whether it's a leading, trailing, or embedded space(s), thus they too must be stripped from the string before using this function, or any of the conversion functions.

boolisNumeric(...);
Allow any object as a parameter

boolisNumeric(TypeInfo[]_arguments, va_list_argptr);
Check only the first parameter, all others will be ignored.

char[]soundex(char[]string, char[]buffer = null);
Soundex algorithm.

The Soundex algorithm converts a word into 4 characters based on how the word sounds phonetically. The idea is that two spellings that sound alike will have the same Soundex value, which means that Soundex can be used for fuzzy matching of names.

Params:
char[]stringString to convert to Soundex representation.
char[]bufferOptional 4 char array to put the resulting Soundex characters into. Ifnull, the return valuebuffer will be allocated on the heap.

Returns:
The four character array with the Soundex result in it. Returnsnull if there is no Soundex representation for thestring.

See Also:
Wikipedia,The Soundex Indexing System

BUGS:
Only works well with English names. There are other arguably better Soundex algorithms, but this one is the standard one.

char[][char[]]abbrev(char[][]values);
Construct an associative array consisting of all abbreviations that uniquely map to the strings invalues.

This is useful in cases where the user is expected to type in one of a known set of strings, and the program will helpfully autocomplete the string once sufficient characters have been entered that uniquely identify it.

Example:
import std.stdio;import std.string;void main() {staticchar[][] list = ["food","foxy" ];auto abbrevs = std.string.abbrev(list);foreach (key, value; abbrevs)    {       writefln("%s => %s", key, value);    } }
produces the output:
 fox => foxy food => food foxy => foxy foo => food


size_tcolumn(char[]string, inttabsize = 8);
Computecolumn number afterstring ifstring starts in the leftmostcolumn, which is numbered starting from 0.

char[]wrap(char[]s, intcolumns = 80, char[]firstindent = null, char[]indent = null, inttabsize = 8);
Wrap text into a paragraph.

The input text strings is formed into a paragraph by breaking it up into a sequence of lines, delineated by \n, such that the number ofcolumns is not exceeded on each line. The last line is terminated with a \n.

Params:
char[]stext string to be wrapped
intcolumnsmaximum number of columns in the paragraph
char[]firstindentstring used to indent first line of the paragraph
char[]indentstring to use to indent following lines of the paragraph
inttabsizecolumn spacing of tabs

Returns:
The resulting paragraph.

char[]isEmail(char[]s);
Does strings[] start with an email address?

Returns:
null it does not char[] it does, and this is the slice ofs[] that is that email address

References:
RFC2822

char[]isURL(char[]s);
Does strings[] start with a URL?

Returns:
null it does not char[] it does, and this is the slice ofs[] that is that URL





Public Domain |Page generated byDdoc.

[8]ページ先頭

©2009-2025 Movatter.jp