Keysight VISA.NET Help
Formatting Strings

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:

  • '-' left aligns the result with the given field width.
  • Quotes introduced by quote modifiers are considered part of the string for the purposes of formatting justification and padding.

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':

  • 'q' places single quotes around the character to be printed.
  • 'Q' places double quotes around the character to be printed.

String Formatting Code Snippets

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' |

 

 

 


© Keysight Technologies 2015-2025