自定义字符串格式#

介绍#

String formatting in C++ for VEX IQ uses printf-style format specifiers to create strings that include variables, expressions, or values. This method uses format specifiers like %d, %f, %s, etc., to insert values into strings.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  int x = 1;
  int y = 5;

  // Display the variables using format specifiers
  Brain.Screen.print("Position: (%d, %d)", x, y);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display a calculation using format specifiers
  Brain.Screen.print("Sum: %d", 5 + 3);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display the robot's battery capacity using format specifiers
  Brain.Screen.print("Battery: %d%%", Brain.Battery.capacity());
}

格式化数字——将值和表达式插入字符串。

  • %d – 格式化整数。

  • %.xf – 设置浮点数显示的小数位数。

  • %x – 将数字格式化为十六进制。

  • %c – 格式化单个字符。

  • %s – 格式化 C 样式字符串。

组合字符串——组合文本和值。

  • strcat - 手动连接 C 样式字符串。

检查子字符串——测试文本的存在或位置。

  • strstr – 在字符串中查找子字符串。

转义序列 – 使用特殊字符格式化输出。

  • \n – 添加换行符(新行)。

  • \t – 在项目之间添加制表符空格。

格式化数字#

C++ 使用格式说明符来控制数字和其他数据类型在字符串中的显示方式:

Integers#

%d formats integer values.

Usage:
%d

参数

描述

该方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  int count = 42;
  Brain.Screen.print("Count: %d", count);
  // Output: Count: 42
}

Fixed Decimal Places#

%.xf controls how many decimal places a floating-point number is displayed with.

Usage:
%.xf

参数

描述

x

要显示的小数位数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display pi with 2 decimal places
  double pi = 3.1415926535;
  Brain.Screen.print("Pi: %.2f", pi);
  // Output: Pi: 3.14
}

Hexadecimal#

%x converts numbers to hexadecimal (lowercase) or %X for uppercase.

Usage:
%x or %X

参数

描述

该方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Convert 255 to hexadecimal
  int number = 255;
  Brain.Screen.print("Hex: %x", number);
  // Output: Hex: ff

  Brain.Screen.print("Hex: 0x%X", number);
  // Output: Hex: 0xFF
}

Characters#

%c formats single characters.

Usage:
%c

参数

描述

该方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  char letter = 'A';
  Brain.Screen.print("Letter: %c", letter);
  // Output: Letter: A
}

Strings#

%s formats C-style strings (character arrays).

Usage:
%s

参数

描述

该方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  char message[] = "IQ";
  Brain.Screen.print("Hello, %s", message);
  // Output: Hello, IQ
}

组合字符串#

strcat can combine two strings together.

**注意:**确保字符串为要附加的字符保留了足够的空间,否则它可能会开始写入属于其他变量的内存。

Usage:
strcat(string1, string2)

参数

描述

string1

要添加的字符串。必须有空间容纳新文本。

string2

您想要添加的字符串。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  char message[] = "Hello,";
  strcat(message, " IQ");

  Brain.Screen.print("%s", message);
  // Output: Hello, IQ
}

检查子字符串#

strstr returns a pointer to the position of the first occurrence of a C-style string in another string.

Usage:
strstr(str, substring)

参数

描述

str

指向正在搜索的字符串的指针。

substring

要搜索的字符串。

Returns: Pointer to the first occurrence, or NULL if not found.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  char message[] = "Hey everyone!";
  if (strstr(message, "Hey") != NULL) {
      Brain.Screen.print("Hello!");
  }
}

转义序列#

转义序列是字符串内部用来格式化文本输出的特殊字符。

New Line#

\n moves text to a new line when printing.

**注意:**这仅适用于控制台。文本只有在开始新行时才会发送到控制台。

参数

描述

该方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display text on two lines
  printf("First line\nSecond line\n");
}

Tab Spacing#

\t inserts a tab space between words or numbers.

**注意:**这仅适用于控制台。文本只有在开始新行时才会发送到控制台。

参数

描述

该方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display the quantity of barrels with tab spacing
  int quantity = 2;
  printf("Barrels:\t%d", quantity);
}