mid() is a member function in the QString class, used to extract substrings from strings

mid() is a member function in the QString class, used to extract substrings from strings.

The following are common uses of the mid() function:

QString str = "Hello, World!";
QString subStr = str.mid(7);  // 返回 "World!"

QString subStr2 = str.mid(3, 5);  // 返回 "lo, W"

In this example, we have a string str that reads "Hello, World!". By calling the mid() function and passing a parameter as the starting index, you can start extracting the substring from that index position.

If only one parameter is passed, everything from the specified index to the end of the string will be extracted. In the first example, we used mid(7) to extract the substring "World!" starting at index position 7.

If two parameters are passed, the first parameter is the starting index, and the second parameter is the number of characters to be extracted. In the second example, we used mid(3, 5) to extract the 5 characters starting at index position 3, the substring "lo, W".

It should be noted that the mid() function returns a copy of the extracted substring and does not modify the original string.

おすすめ

転載: blog.csdn.net/m0_46376834/article/details/134910451