QT中正则表达式过滤时间以及string编码转换

#include "mainwindow.h"
#include 
#include
//#pragma execution_character_set("utf-8")  //放开这句话
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
#include
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   QString str = QString("2012年11月09日  11时23分36秒  师父父");
   qDebug()<<"----------------------"<<endl;
    qDebug()<<"str to utf8 "<<str.toUtf8();
    qDebug()<<"sssssssssss"<<QString::fromLocal8Bit(str.toUtf8());
    qDebug()<<"str to toLatin1 "<<str.toLatin1();
    qDebug()<<"str to toLocal8Bit "<<str.toLocal8Bit();
   //QString src = UTF82GBK(str.toUtf8());
//    QString str = QStringLiteral("2012-11-09 11:14:13  wtg");
// QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
//    QString str_utf8 =  utf8->toUnicode(str.toUtf8());

    //qDebug()<<"_____________________________"<
   // QRegExp rg("^\\d{4}-\\d{1,2}-\\d{1,2}");
 //   QRegExp rg("^\\d{4}(-|[\\u4e00-\\u9fa5]{0,})\\d{1,2}(-|[\\u4e00-\\u9fa5]{0,})\\d{1,2}(\\s|[\\u4e00-\\u9fa5]{0,})\\s{0,}\\d{1,2}(:|[\\u4e00-\\u9fa5]{0,})\\d{1,2}(:|[\\u4e00-\\u9fa5]{0,})\\d{1,2}\\s{0,}");
    QRegExp rg("^\\d{4}\\D*\\d{1,2}\\D*\\d{1,2}\\D*\\s{0,}\\d{1,2}\\D*\\d{1,2}\\D*\\d{1,2}(\\s|\\D)\\s{0,}");
//QRegExp rg("^\\d{4}(-|[^\\x00-\\xff])\\d{1,2}(-|[^\\x00-\\xff])\\d{1,2}[^\\x00-\\xff]\\s{0,}\\d{1,2}(:|[^\\x00-\\xff])\\d{1,2}(:|[^\\x00-\\xff])\\d{1,2}\\s{0,}");
    //str = str.toLocal8Bit().remove(rg);
      // qDebug()<<"index is "<
  //   src = QString(str.toUtf8().toStdString().data()).remove(rg);
    //QString st = QString::fromLocal8Bit(src.toUtf8());
    //qDebug()<<"_____________________________"<
   qDebug()<<"_____________________________"<<str;
//   cout<
 //  src = UTF82GBK(src.toUtf8());
  // qDebug()<<"_____________________________"<
    qDebug()<<"____________________________________________________";
  char tw[512] = "2011年11月09日11时23分36  王微微";
  // char tw[512] = "2011-11-09  11:23:36    中国年";
   qDebug()<<"the char [] is "<<tw;
   QString tw_str = QString::fromLocal8Bit(tw);
   qDebug()<<"the str1   is "<<tw_str;
    if(tw_str.contains("年"))
        qDebug()<<"!!!!!!!!!!!!!!!!!!!!!!!!";
//   QString src1  = GBK2UTF8(tw_str.toUtf8());
//   qDebug()<<"after tarn "<
//   qDebug()<<"UTF82GBK  : "<
  tw_str = tw_str.remove(rg);
  if(tw_str.contains(rg))
      qDebug()<<"wtg";
   //tw_str = QString(tw_str.toStdString().data()).remove(rg);
   qDebug()<<"remove str  is "<<tw_str;
}

MainWindow::~MainWindow()
{
    delete ui;
}
 QString MainWindow::GBK2UTF8(QByteArray &inStr)
{
    QTextCodec *gbk = QTextCodec::codecForName("gbk");
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

    char *p = inStr.data();
    QString str = gbk->toUnicode(p);
    qDebug()<<"GBK2UTF8 "<<str;
    QByteArray utf8_bytes=utf8->fromUnicode(str);
    p = utf8_bytes.data();
    str = p;
    qDebug()<<"GBK2UTF8 "<<str;
    return str;

}

 QString MainWindow::UTF82GBK(QByteArray &inStr)
{
    QTextCodec *gbk = QTextCodec::codecForName("gbk");
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

    char *p = inStr.data();
    QString str = utf8->toUnicode(p);

    QByteArray utf8_bytes=gbk->fromUnicode(str);
    p = utf8_bytes.data();
    str = p;

    return str;
}

你可能感兴趣的:(qt,qt,开发语言)