| Using VISA.NET > Formatted I/O > Using Printf > Printf Code Snippets > Formatting Strings |
A string argument has a format specifier of the following form:
%[flags][width][.precision][,[arraySize]]s
|
Modifier |
Interpretation |
|---|---|
|
Default Functionality |
The argument is interpreted as a string or array of strings. |
|
flags - |
Controls justification and padding of the output, as follows:
|
|
width |
Minimum field width of the output string. An asterisk (*) may be present in lieu of an integer width modifier, in which case an extra Int32 argument supplies the value. Quotes introduced by quote modifiers are considered part of the string for the purposes of formatting width. |
|
.precision |
Maximum number of characters to send. An asterisk (*) may be present in lieu of an integer precision modifier, in which case an extra Int32 argument supplies the value. Quotes introduced by quote modifiers are considered part of the string for the purposes of formatting width. |
|
[,[arraySize]] |
Formats an array of strings. If arraySize is specified, it is the maximum number of elements to include in the formatted array. An asterisk (*) may be present in lieu of an integer arraySize modifier, in which case an extra Int32 argument supplies the value. |
|
Quote Modifiers Q,q |
Quote modifiers immediately precede the type specifier 's':
|
Remember that Printf is adding characters to the formatted write buffer. The comments after each Printf call show what character(s) are added to the formatted write buffer by that call. Assume that the io variable is a valid reference to IMessageBasedFormattedIO.
| Literal Text and Escape Sequences |
Copy Code |
|---|---|
io.Printf("|Hello World|"); // Adds |Hello World| io.Printf("|Hello World|\n"); // Adds |Hello World| & sends to device io.Printf(@"|\123|"); // Adds |S| | |
| No Modifiers |
Copy Code |
|---|---|
io.Printf("|%s|", "Hello World"); // Adds |Hello World| io.Printf("|%s|\n", "Hello World"); // Adds |Hello World| & sends to device | |
| Width and Precision Modifiers |
Copy Code |
|---|---|
io.Printf("|%15s|", "Hello World"); // Adds | Hello World| io.Printf("|%-*s|", 15, "Hello World"); // Adds |Hello World | io.Printf("|%.5s|", "Hello World"); // Adds |Hello| | |
| Array Modifiers |
Copy Code |
|---|---|
String[] hi = new String[] {"Hi","Hi","Hi"}; io.Printf("|%,s|", hi); // Adds |Hi,Hi,Hi| io.Printf("|%,qs|", hi); // Adds |'Hi','Hi','Hi'| io.Printf("|%-5,2qs|", hi); // Adds |'Hi' ,'Hi' | | |