Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common methods of QString

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the common methods of QString have related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have some gains after reading this QString common methods, let's take a look at it.

Brief introduction

QString strings are used by every GUI program, not only in the user interface, but also in data structures.

C++ natively provides two kinds of strings: the traditional C-style character array that ends with''and the std::string class. Unlike these, QString uses 16-bit Unicode values when using QString, we don't have to worry about such secretive details, such as allocating enough memory or data ending with''. In general, QString can be thought of as a QChar vector. A QString can embed the''character. The length () function returns the entire string size, including the embedded''.

Append string

QString provides a binary + operator to concatenate two strings and a + = operator to append a string to a string.

For example:

QString str = "A"; QString str2 = "D"; str = str + "B"; str2 + = "E"

The result is that str is "AB" and str2 is "DE"

Combination string

(1) use the sprintf () function of QString

For example:

QString str;str.sprintf ("% s% .1f%%", "Value", 100.0)

As a result, str is assigned to "Value 100.0%".

(2) use arg ()

For example:

QString str;str = QString ("% 1 aa% 2") .arg ("aa") .arg ("bb")

The result str is "aa,bb"

Convert numbers to strings

(1) use the static function QString::number () for example:

Str = QString::number (38.5)

(2) use the setNum () function

Str.setNum (38.5)

Convert a string to a number

Using toInt (), toLongLong (), toDouble ()... Wait.

For example:

QString str = "12"; int I = str.toInt ()

Extract string

(1) the mid () function returns a string with a given starting position (first argument) and length (second argument).

For example:

QString str = "white man"; QString str2 = str.mid (6,2)

The result str2 is "ma"

(2) if the second argument is omitted, mid () returns the substring from the specified starting position to the end of the string.

QString str = "white man"; QString str2 = str.mid (6)

The result str2 is "man"

(3) the left () function returns the first n characters

QString str = "white man"; QString str2 = str.left (5)

The result str2 is "white"

(4) the right () function returns the last n characters

QString str = "white man"; QString str2 = str.right (3)

The result str2 is "man"

Determine whether the string contains

If we want to find out whether a string contains a character, a string, or a regular expression, we can use the indexOf () function of QString.

QString str = "white man"; int I = str.indexOf ("man")

As a result, I is 6, or-1 if it does not exist.

Determine whether a string begins or ends with something

(1) start with something and use the startsWith () function

QString str = "http:www.baidu.com"; int I = str.startsWith ("http:")

The result is I = 1

(2) end with something and use the endsWith () function

QString str = "http:www.baidu.com"; int I = str.endsWith ("com")

The result is I = 1

Case switching

(1) switch to uppercase and use the toUpper () function

QString str = "FileName"; QString str2 = str.toUpper ()

The result str2 is "FILENAME"

(2) switch to lowercase and use the toLower () function

QString str = "FileName"; QString str2 = str.toLower ()

The result str2 is "filename"

String substitution

(1) replace a part of a string with another string, you can use the replace () function

QString str = "white man"; str.replace (0,5, "Black")

The result str is "Black man"

(2) delete the m characters starting from position n and use the remove (NMagne m) function

QString str = "white man"; str.remove (6meme 3)

The result str is "white"

(3) insert a string in position n and use the insert (int, QString) function

QString str = "white man"; str.insert (6, "strong")

The result str is "white strong man"

Filter white space characters

Remove white space from both ends of the string and use trimmed ()

QString str = "white man"; QString str2 = str.trimmed ()

The result str2 is "white man"

Split string

A string can be divided into a QStringList with a substring, by using QString::split ()

QString str = "white black yellow"; QStringList color = str.split ("")

Results color.at (1) is "white", color.at (2) is "black", and color.at (3) is "yellow"

Determine whether the string is empty

This can be achieved by calling isEmpty () or checking that length () is 0.

If (str.isEmpty ()) {} if (str.length () = = 0) {}

Const char * converted to QString

(1) implicit conversion (automatic)

For example:

Str = "(121)"

(2) explicit conversion

Simply use a QString cast, or call the fromAscii () or fromLatin1 () function

QString to const char *

Use toAscii () or toLatin1 (). These functions return a QByteArray that can be converted to a const char * by using QByteArray::data () or QByteArray::constData ().

For example:

QString = "(1178)" str.toAscii (). Data ()

This is the end of the article on "what are the common methods of QString?" Thank you for reading! I believe you all have a certain understanding of "what are the common methods of QString". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report