自定义字符串格式#

介绍#

String formatting in C++ for VEX V5 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.

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

  • %d — Formats integers.

  • %.xf — Sets the number of decimal places to show for floats.

  • %x — Formats a number as hexadecimal.

  • %c — Formats single characters.

  • %s — Formats C-style strings.

合并字符串 — 将文本和值合并在一起。

  • strcat — Concatenate C-style strings manually.

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

  • strstr — Finds a substring within a string.

转义序列——用于格式化包含特殊字符的输出。

  • \n — Adds a line break (new line).

数字格式化#

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

Integers#

%d formats integer values.

Usage:
%d

参数

描述

此方法没有参数。

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

  int count = 42;

  // Display a single variable 
  Brain.Screen.print("Count: %d", count);
}

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

  int x = 1;
  int y = 5;

  // Display multiple variables
  Brain.Screen.print("Position: (%d, %d)", x, y);
}

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

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

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

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

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);
}

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;

  // Display hexadeximals 
  Brain.Screen.print("Hex: %x", number);

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

Characters#

%c formats single characters.

Usage:
%c

参数

描述

此方法没有参数。

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

  char letter = 'A';

  // Display a single character variable
  Brain.Screen.print("Letter: %c", letter);
}

Strings#

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

Usage:
%s

参数

描述

此方法没有参数。

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

  char message[] = "V5";

  // Display a string variable
  Brain.Screen.print("Hello, %s", message);
}

字符串合并#

strcat can combine two strings together.

**注意:**请确保字符串预留了足够的空间来添加字符。如果字符串空间过小,添加字符可能会覆盖其他变量,导致意外行为。

Usage:
strcat(string1, string2)

参数

描述

string1

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

string2

你想添加的字符串。

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

  // Make sure the variable has enough space
  // for all characters (set it to 20)
  char message[20] = "Hello,";
  strcat(message, " V5");

  // Display the combined strings
  Brain.Screen.print("%s", message);
}

检查子字符串#

strstr returns a pointer to the position of the first occurrence of a C-style string in another string. This will return NULL if the string is not found.

Usage:
strstr(str, substring)

参数

描述

str

指向要搜索的字符串的指针。

substring

要搜索的字符串。

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

  char message[] = "Hey everyone!";

  // Display if "Hey" is in the variable
  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");
}