RAS プレイグラウンド English GitHub

RAS標準ライブラリ

文字列

文字列の拡張メソッドです(RAS標準ライブラリの ras_text)。インポートしなくても、どのモジュールからも s.width() のように呼べます。使ったメソッドだけが VBA のモジュール RAS_Text に出力されます。

export function fixedWidth(name: string, price: int): string {
    // 半角 1・全角 2 で数えて、20 桁に切って右を空白で埋める
    const head = name.leftWidth(20);
    return head + " ".repeat(20 - head.width()) + `${price}`;
}

export function similar(a: string, b: string): boolean {
    return a.countMatchChars(b) * 2 >= a.length;
}

組み込みの文字列のメソッド(split・trim・includes・replace など)は 早見表 と仕様書の §15.4.1 を参照してください。split は正規表現(s.split(/\s*,\s*/))でも区切れ、trim は取り除く文字を指定できます("  x  ".trim("  "))。

リファレンス

拡張メソッド(string) string.width

function width(this: string): int

半角の文字を 1、全角の文字を 2 と数えた文字列の幅(Shift_JIS のバイト数と同じ。固定長のテキストの桁数に使う)。半角は ASCII の文字と半角カタカナ(ア・゙・。 など)。

例: "カナ漢字abc".width() → 9

ras_text.ras

拡張メソッド(string) string.leftWidth

function leftWidth(this: string, n: int): string

先頭から幅(半角 1・全角 2)n に収まる部分。全角の文字の半分だけが収まるときは、その文字を含めない。

例: "漢字abc".leftWidth(3) → "漢"、"abc".leftWidth(5) → "abc"

ras_text.ras

拡張メソッド(string) string.rightWidth

function rightWidth(this: string, n: int): string

末尾から幅(半角 1・全角 2)n に収まる部分。全角の文字の半分だけが収まるときは、その文字を含めない。

例: "abc漢字".rightWidth(5) → "c漢字"

ras_text.ras

拡張メソッド(string) string.midWidth

function midWidth(this: string, start: int, n: int = -1): string

幅(半角 1・全角 2)で数えた位置 start(0 から)から、幅 n に収まる部分(n を省くと末尾まで)。 start の位置に全角の文字の後半が掛かるときは、その文字を含めない。

例: "12漢字34".midWidth(2, 4) → "漢字"、"12漢字34".midWidth(3) → "字34"

ras_text.ras

拡張メソッド(string) string.toValue

function toValue(this: string): any

文字列を、内容に合う型の値にする(CSV・テキストファイルから読んだ値などに)。 "TRUE" / "FALSE"(大文字小文字を問わない)は真偽値、数値として読めるもの(全角の数字・桁区切りも)は double、日付として読めるもの("2026/4/1")は date、それ以外はそのままの文字列。

例: "1,200".toValue() → 1200、"2026/4/1".toValue() → 日付、"abc".toValue() → "abc"

ras_text.ras

拡張メソッド(string) string.countMatchChars

function countMatchChars(this: string, other: string): int

2 つの文字列に共通する文字の数(順序は問わない。同じ文字は、両方に含まれる数だけ数える)。表記の揺れた名前どうしの近さを測るのに使う。

例: "東京都港区".countMatchChars("港区東京") → 4

ras_text.ras

拡張メソッド(string) string.longestCommonSubsequence

function longestCommonSubsequence(this: string, other: string): string

2 つの文字列の最長共通部分列(両方に同じ順で現れる、最も長い文字の並び。間に別の文字があってもよい)。

例: "ABCBDAB".longestCommonSubsequence("BDCABA") → "BDAB"

ras_text.ras