General and Desktop
2019.01.26 17:57

QString 문자열 다루기 예제

조회 수 47003 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
QString이 제공하는 주요 함수, 기능들로 문자열 다루는 방법을 예제를 통해 살펴보자.

문자열 초기화 및 정의
QString makersweb("makersweb.net");
QString str1 = "This is QString";
QString str2{"foobar"};

 

문자열의 길이, 사이즈, 카운트

QString str("macaron");

int length = str.length();
// length == 7

int size = str.size();
// size == 7

int count = str.count();
// count == 7

 

문자열 뒤에 이어 붙이기, 앞에 붙이기

// auto keyword from C++11
auto url = QString("makersweb");
url.append(".net");
// url == "makersweb.net"

url.prepend("https://");
// url == "https://makersweb.net"

 

null 과 empty 체크

QString address;

bool ret = address.isEmpty();
// ret == true
ret = address.isNull();
// ret == true

address = "";

ret = address.isEmpty();
// ret == true
ret = address.isNull();
// ret == false

 

split 특정문자를 기준으로 문자열 분리(자르기, 나누기)

QString address = "192.168.0.1";

QStringList list = address.split(".");
// list == ("192", "168", "0", "1")

 

문자열 치환

QString year    = "2025";
QString month   = "11";
QString day     = "20";

auto date = QString("%1. %2. %3").arg(year).arg(month).arg(day);
// date == "2025. 11. 20"

date = "%3. %2. %1";
auto newformat = date.arg(year, month, day);
// newformat == "20. 11. 2025"

 

지정된 인덱스 위치에있는 문자 반환

QString str("no smoking");

QChar c = str.at(3);
// c == 's'

 

문자열의 마지막 또는 앞의 문자 반환

QString str("dessert");

QChar c = str.back();
// c == 't'

c = str.front();
// c == 'd'

 

문자열의 끝에서 입력 수 만큼 제거

QString filename("file.txt");

filename.chop(3);
// str == "file."

 

문자열의 끝에서 입력 수 만큼 뺀 앞의 문자열을 반환

QString filename("config.ini");

auto name = filename.chopped(4);
// name == "config"

 

문자열의 왼쪽 또는 오른쪽 끝에서 입력 수 만큼의 문자열을 반환

QString str = "FREE WIFI";

QString left = str.left(4);
// left == "FREE"

QString right = str.right(4);
// right == "WIFI";

 

문자열의 내용을 지우고 null로 만듦.

QString text("text");

text.clear();
// text.isNull() == true
// text == ""

 

숫자를 문자열로, 문자를 숫자로 변환

auto str = QString::number(123);
// str == "123"

int num = str.toInt();
// num == 123

 

문자열에 특정 문자열 포함 여부(검색)

QString str = "Don't stop.";

bool ret = str.contains("don't", Qt::CaseInsensitive); // 대소문자 구분 없이
// ret == true

 

문자열에서 특정 문자열의 시작 위치(인덱스)

QString str = "Don't stop.";

int index = str.indexOf("stop", Qt::CaseInsensitive);
// index == 6

index = str.indexOf("go", Qt::CaseInsensitive);
// index == -1

 

C++ 표준 문자열로, 표준 문자열을 QString으로 변환

QString str("standard");

std::string stdstring = str.toStdString();

QString qstring = QString::fromStdString(stdstring);

 

특정 문자로 채움

QString str;

str.resize(10);
str.fill('*');
// str == "**********"

str.fill('@', 5); // string is resized(리사이징됨)
// str == "@@@@@"

 

특정 위치에 문자열 끼워넣기

QString str = "https://makersweb.net";

str.insert(8, QString("www."));
// str == "https://www.makersweb.net"

 

지정 위치부터 입력 수 만큼 삭제

QString str = "https://www.makersweb.net";

str.remove(8, 4);
// str == "https://makersweb.net"

 

문자열이 대문자 또는 소문자로 이뤄져 있는지 여부 반환. 문자열을 대, 소문자로 변환.

QString str("Foobar");

str.isUpper();
// false
str.isLower();
// false

auto upper = str.toUpper();
auto lower = str.toLower();

upper.isUpper();
// true
lower.isLower();
// true

 

특정 위치부터 지정 길이만큼 다른 문자열로 바꾸기

인덱스 위치에서 시작하는 n 개의 문자를 이후의 문자열로 대체하고이 문자열에 대한 참조를 반환.

QString str("you can't do it.");
QString can("can");

str.replace(4, 5, can);
// str == "you can do it."

 

앞의 문자를 뒤의 문자로 교체하기

QString address("08_AE_D6_B0_6F_E6");

address.replace("_", ":");
// address == "08:AE:D6:B0:6F:E6"

 

문자열의 한 섹션(특정 부분)을 반환.

필드는 왼쪽에서부터 0, 1, 2 등으로 번호가 매겨지며, 오른쪽에서 왼쪽으로는 -1, -2 등으로 번호가 매겨짐.

QString path = "/home/user/workspace/file.txt";

QString fileName = path.section("/", -1);
// fileName == "file.txt"

QString location = path.section("/", 0, -2);
// location == "/home/user/workspace"

 

참고: http://doc.qt.io/qt-5/qstring.html


  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views109911
    read more
  2. 가상키보드(Qt Virtual Keyboard)를 사용하는 방법

    Date2019.05.03 CategoryMobile and Embedded Bymakersweb Views235056
    Read More
  3. Windows에서 Qt D-Bus를 사용하여 프로세스간 통신(IPC)

    Date2019.05.02 CategoryGeneral and Desktop Bymakersweb Views7326
    Read More
  4. No Image

    QML과 QtQuick 모듈 개념과 기본 타입들

    Date2019.04.26 CategoryQML and Qt Quick Bymakersweb Views15491
    Read More
  5. QML 전역 객체 (Global Object)

    Date2019.04.10 CategoryQML and Qt Quick Bymakersweb Views2680
    Read More
  6. tslib의 ts_calibrate를 응용해서 Qt로 터치보정기능 구현

    Date2019.04.06 CategoryQML and Qt Quick Bymakersweb Views3101
    Read More
  7. No Image

    GPU가 없는 장치에서 Qt Quick을 사용

    Date2019.04.02 CategoryQML and Qt Quick Bymakersweb Views4259
    Read More
  8. No Image

    QTextCodec클래스를 사용하여 유니코드와 EUC-KR 변환

    Date2019.03.25 CategoryGeneral and Desktop Bymakersweb Views5928
    Read More
  9. No Image

    qInstallMessageHandler를 이용한 디버그 메세지 출력 제어하기

    Date2019.02.25 CategoryGeneral and Desktop Bymakersweb Views3604
    Read More
  10. Qt5기반 독립 프로세스(out-of-process)로 동작하는 가상키보드(virtual keyboard)

    Date2019.02.24 CategoryMobile and Embedded Bymakersweb Views5096
    Read More
  11. Qml 기본 컴포넌트 강좌 (4) - 모델 리스팅(Listing)

    Date2019.02.23 CategoryQML and Qt Quick By운영자 Views7377
    Read More
  12. Qt Bluetooth를 이용한 시리얼(Serial) 통신

    Date2019.02.17 CategoryMobile and Embedded Bymakersweb Views6291
    Read More
  13. Qml 기본 컴포넌트 강좌 (3) - 배치(positioning) 컴포넌트

    Date2019.02.10 CategoryQML and Qt Quick By운영자 Views7684
    Read More
  14. No Image

    QString 문자열 다루기 예제

    Date2019.01.26 CategoryGeneral and Desktop By운영자 Views47003
    Read More
  15. Qt SQL을 이용한 가벼운 데이터베이스 다루기

    Date2019.01.23 CategoryGeneral and Desktop By운영자 Views9500
    Read More
  16. 구글 클라우드 Speech-To-Text API를 Qt기반(C++, Qml)테스트

    Date2019.01.20 CategoryGeneral and Desktop Bymakersweb Views5120
    Read More
  17. No Image

    QNetworkAccessManager를 통해 HTTP POST 하는 예제

    Date2019.01.17 CategoryGeneral and Desktop Bymakersweb Views7093
    Read More
  18. Qt응용프로그램 실행 시 콘솔창(터미널)같이 띄우기

    Date2019.01.16 CategoryGeneral and Desktop Bymakersweb Views7195
    Read More
  19. 안드로이드 가상장치 사용

    Date2019.01.13 CategoryMobile and Embedded Bymakersweb Views3328
    Read More
  20. No Image

    Qml 기본 컴포넌트 강좌 (2)

    Date2019.01.05 CategoryQML and Qt Quick Bymakersweb Views10929
    Read More
  21. Qml 기본 컴포넌트 강좌 (1)

    Date2019.01.03 CategoryQML and Qt Quick Bymakersweb Views15216
    Read More
Board Pagination Prev 1 5 6 7 8 9 Next
/ 9