LoginSignup
0
0

More than 5 years have passed since last update.

C++ Builder XE4 > String > 共通するPostfix文字列を取得する > getCommonPostfixString()

Last updated at Posted at 2018-03-08
動作環境
C++ Builder XE4

処理概要

  • 1 倍速, 2倍速
    • >> "倍速"を返す
  • 30 小人, 25 小人
    • >> "小人"を返す
  • 1024 バイト, 20M バイト
    • >> "バイト"を返す

code

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

static String getCommonPostfixString(String strA, String strB)
{
    for (int idx=0; idx < strA.Length(); idx++) {
        String wrkStr = strA.SubString(idx, MAXINT); // MAXINT: 最後まで
        if (strB.Pos(wrkStr) > 0) {
            return wrkStr;
        }
    }

    return L"";
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    ShowMessage( getCommonPostfixString(L"1 倍速", L"2 倍速") );

    ShowMessage( getCommonPostfixString(L"30 小人", L"25 小人") );

    ShowMessage( getCommonPostfixString(L"1024 バイト", L"20M バイト") );
}
//---------------------------------------------------------------------------

実行例

qiita.png

qiita.png

qiita.png

用途

TComboBoxのItemsで定義した文字列のうち、単位だけを取り除きたい。

単位はgetCommonPostfixString()で取得する。

このようなソフト実装にしておくと、フォーム上のデザイン(TComboBoxのItemsの定義)を変更した後でもソフトが壊れない。

デザインの変更とコードの変更、両方を行わないといけない状況の回避案。
(修正箇所が増えると失敗し、デバッグ時間が長くなる)。

まじめな実装 (共通文字列の取得)

共通文字列の取得のまじめな実装をするとしたら、下記のリンクのリンク先にあるDelphiコードをC++ Builder用に実装しなおせばよいだろう。

やる気がでない7of9。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0