The QT string string string operation and a division operation

https://blog.csdn.net/rong11417/article/details/90203837

1. split the specified character segmentation, such as in accordance with ";" or "/" division, etc.


QString str = "hello,world"
QStringList list = str.split(",");
QString a = list[0]; //a = "hello"
QString b = list[1]; //b = "world"
QString str2 = b.append(a);
str2 = "worldhello"


2. mid divided by location

Parameter indicates a position to be taken from the end of the string

It represents two parameters between the two positions taken string


QString str = "helloworld"
QString a = str.mid(0, 5);
QString b = str.mid(5);
QString str2 = b.append(a);
str2 = "worldhello"

 

QString class using the QT

The role of function QString class.

A string concatenation function.
1, QString also overloaded + and + = operator. These operators can be connected to two strings together.
    
2, QString the append () function provides a similar operation, for example:

   str = "User: ";  
   str.append(userName);  
   str.append("\n");


Second, obtaining a location value of a character string.

//

QString has no function can be taken out of the string between the two specified positions?

 
    QString x = "Nine pineapples";  
    QString y = x.mid(5, 4);            // y == "pine"  
    QString z = x.mid(5);               // z == "pineapples"

 

1, mid () function accepts two arguments, the first is the start position, the second is to take the length of the string. If the second parameter is omitted, it will be taken from the starting position to the end. As the example shown above as

2, the function left () and rigt () Similarly, accepts a parameter of type int n, the string are taken. Except that, left () function, taken from the left n characters, and the right () taken from the right side. The following is an example of left () is:

   1. QString x = "Pineapple";  
   2. QString y = x.left(4);      // y == "Pine"

Third, access to positions of characters in the character.
     indexOf ()
   // there is lastIndexOf () function returns the index of the last occurrence of the string?  
   QString the X-= "Sticky Question";  
   QString the y-= "the STI";  
   x.indexOf (the y-); // returns A 0  
   x.indexOf (Y,. 1); // Returns 10  
   x.indexOf (Y, 10); // Returns 10  
   x.indexOf (Y,. 11); // Returns -1

Fourth, the string is not possible to detect a specific sequence in the beginning or end.
    startsWith () endsWith ()
    
    
    IF (url.startsWith ( "HTTP:". "PNG") && url.endsWith ())  
         {}

    This code is equivalent to

   if (url.left(5) == "http:" && url.right(4) == ".png") 
         {  }

Fives,

     String replacement function Replace ();
     OF TRIMMED () function is removed on both sides of blank character string (note blank characters including spaces, Tab and line breaks, rather than a space);
     toLower () and the toUpper () function will uppercase to lowercase string into a character string;
     remove () and insert () functions provide the ability to insert and delete strings;
     Simplified () function all may be replaced with a continuous string of blanks, and the ends of the blank removing characters such as "\ t" returns a space "."

 

Sixth, const char * conversion between types of C-style strings and QString characters.

     Briefly, QString = a + to accomplish this function:
     STR + = "(1870)";

     Here, we will type const char * string "(1870)," converted into a QString type.
     If you need an explicit conversion, may be used QString cast operation or function used fromAscii () and the like.
     In order to turn into a type QString const char * string, requiring two steps, one using ToASCII () to obtain a QByteArray type object,
     and then call its data () or constData () function,
     for example:

     printf("User: %s\n", str.toAscii().data());

     For convenience, Qt provides a macro qPrintable (), the macro is equivalent to toAscii () constData (), for example:

      printf ( "User:% s \  n", qPrintable (str));
      
      we call class QByteArray above data () or constData () function and obtain a const char * string inside QByteArray type,
      therefore, we do not need to worry about memory leaks and other problems, Qt will be for us to manage memory. But this also implies that we, careful not to use the pointer for too long,
      because if QByteArray is delete, then the pointer will become a field guide. QByteArray If this object is not placed in a variable,
      then when the end of the statement, QbyteArray the object will be delete, delete the pointer will be.

Seven other types of character string conversion function.
     toInt () transfected integer
     toDouble () transfected double
     toLong () Long turn
     these functions bool accepts a pointer as a parameter, after the conversion is successful based on whether the function is set to true or false:

     bool ok;  
     double d = str.toDouble(&ok);  
     if(ok)   
     {  
         // do something...  
     } else {  
          // do something...  
     }


     Integer rotation string:
         1, using the static function number () can be converted into a digital string. For example:
             QString QString :: = Number STR (54.3); 
         2, may be used non-static function setNum () to accomplish the same objectives:
              QString STR;  
              str.setNum (54.3);


    

Eight, QString provides a sprintf () function is achieved with the C language printf function the same functionality.
    
    1. str.sprintf ( "% s% .1f  %%", "perfect competition", 100.0);
       this code output: 100.0% Perfect Competition
       
    2, another format string arg function output ():
 
        str = QString ( "% 1% 2 (% 3s-% 4s)") arg ( "permissive") arg ( "society") arg (1950) .arg (1970)...;

{

 

QString str;
str ="%1 %2";
str.arg("%1f","Hello");       // returns "%1f Hello"
str.arg("%1f").arg("Hello");   // returns "Hellof %2"

 

 

}

      In this code, 1%, 2%, 3%, 4%, as a placeholder, to be later Arg () function are sequentially replacing the contents, such as 1% is to be replaced to permissive,
      % 2 will be replaced society,% 3 1950 will be replaced, is replaced with 4% was 1970, and finally,
      this code is output:. permissive society (1950s-1970s ) arg () function than sprintf () to be type-safe,
      while it also accepts a variety of data types as a parameter, it is recommended to use Arg () function rather than the conventional sprintf ().

Nine, find the string length, the return value type INT.
     length ();

Ten, how to display Chinese characters correctly

If the character set based on the QT made Locale environment variables, you can use the following command:
QString :: fromLocal8Bit ( "Hello, world!");

Using a QString fromLocal8Bit () function;


QString STR;


STR = str.fromLocal8Bit ( "ha");

Published 42 original articles · won praise 148 · views 410 000 +

Guess you like

Origin blog.csdn.net/baidu_37503452/article/details/104359203