プログラム言語 標準関数(テキスト系)

入出力

入力

標準入力(StandardInの略)値を取得
my $str = <STDIN>

my @str = ();
for my $inpcnt (0..2){
 $str[$inpcnt] = <STDIN>;
 chomp $str[$inpcnt];
}

出力

using System.Windows.Forms;

※メッセージのみ
MessageBox.Show(text: “Hello”);

※+キャプション(Window上部)
MessageBox.Show(
  text: “Hello”,
  caption: “caption”);

※+ボタン
MessageBox.Show(
  text: “Hello”,
  caption: “caption”,
  buttons:MessageBoxButtons.YesNoCancel);

MessageBoxButtons列挙体メンバー一覧
OK
「OK」ボタンのみ。
OKCancel
「OK」/「キャンセル」ボタン。
AbortRetryIgnore
「中止」/「再試行」/「無視」ボタン。メッセージボックスの閉じるボタンが無効。
YesNoCancel
「はい」/「いいえ」/「キャンセル」ボタン。
YesNo
「はい」/「いいえ」ボタン。メッセージボックスの閉じるボタンが無効。
RetryCancel
「再試行」/「キャンセル」ボタン。

※ボタン操作結果を取得
DialogResult answer = MessageBox.Show(text: “Hello”,
  caption: “caption”,
  buttons:MessageBoxButtons.YesNoCancel);

if (answer == DialogResult.Yes)
{
  ~
}

※+アイコン
MessageBox.Show(text: “Hello”,
  caption: “caption”,
  buttons: MessageBoxButtons.YesNoCancel,
  icon:MessageBoxIcon.Information);

MessageBoxIcon列挙体メンバ一覧
None
アイコンなし
音:一般警告音

Hand
赤丸に白いX(停止マーク)。
音:システムエラー

Question
丸い吹き出しに疑問符記号。
音:メッセージ(問い合わせ)

Exclamation
黄色い三角に感嘆符記号。
音:メッセージ(警告)

Asterisk
丸い吹き出しに「i」。
音:メッセージ(情報)

Stop
Handと同じ。
Error
Handと同じ。
Warning
Exclamationと同じ。
Information
Asteriskと同じ。

#include <iostream>

std::cout << 5 << ‘\n’;
std::cout << 5 << std::endl;

※メッセージのみ
MessageBox.Show(text:=”Hello”)

※+キャプション(Window上部)
MessageBox.Show(text:=”Hello”,
  caption:=”caption”)

※+ボタン
MessageBox.Show(text:=”Hello”,
  caption:=”caption”,
  buttons:=MessageBoxButtons.YesNoCancel)
  
※MessageBoxIcon列挙体メンバ一覧は「C#」参照

※ボタン操作結果を取得
Dim answer As DialogResult = MessageBox.Show(text:=”Hello”,
  caption:=”caption”,
  buttons:=MessageBoxButtons.YesNoCancel)

If answer = DialogResult.Yes Then
  ~
End If

※+アイコン
MessageBox.Show(text:=”Hello”,
  caption:=”caption”,
  buttons:=MessageBoxButtons.YesNoCancel,
  icon:=MessageBoxIcon.Information)
  
MessageBoxIcon列挙体メンバ一覧は「C#」参照

print
#標準出力
print “test”;
print “test\n”;
print “test”,”\n”;
my $es = “es”;
print “t”,$es,”t”,”/n”
print “t$est”,”/n”
⇒test

say
#標準出力。自動で改行される。
say “test”;
⇒test

テキスト操作

StringBulder

String型の変数にリテラルを格納する場合、
変数、リテラル両方のメモリ領域を必要とする。
変数に別のリテラルを格納する為には更に別のリテラル用のメモリ領域も必要で非効率。
String s;
s = “aaa”
s = “bbb”
※”aaa” と “bbb” は別のメモリ領域
※”aaa” はどこからも参照される事なく無駄に残り続ける。

メモリに関して効率良く文字列を管理する為のクラスがStringBuilder
内部ではchar型配列の組み合わせを外部からはString型の様に利用できる。
String型の値を頻繁に書き換える必要がある場合に使用。

StringBuilder sb = new StringBuilder(“aa”);
sb : aa
sb.toString() : aa
sb.capacity() : 18;
//16文字分の配列のバッファ + aa(2文字)分の配列のバッファ

sb.append(“bb”);
// sb : aabb

sb.append(“cc”);
// sb : aabbcc

sb.insert(2, “dd”);
// index : 0a1a2b3b4c5c6
// sb : aaddbbcc

sb.delete(4, 6);
// index : 0a1a2d3d4b5b6c7c8
// sb : aaddcc

sb.deleteCharAt(0);
sb.deleteCharAt(0);
// sb : ddcc

sb.reverse();
// sb : ccdd

sb.replace(1,3, “zz”);
// sb : czzd

//メソッドチェーンによる記述も可
sb.append(“cc”).insert(0, “ee”);

複写

#include <cstring>

using namespace std;

char c[50];

要素数が定義されている文字配列にコピー
strcpy_s(c, 50, “VisualC++”);
cout << c << ‘\n’;
→VisualC++

↓ も可(要素数省略)
strcpy_s(c, “VisualC++”);
cout << c << ‘\n’;
→VisualC++

char *myStr = “VisualC++”;
char c[50];

strcpy_s(c, 50, myStr);
cout << c << ‘\n’;
→VisualC++

char c*;

strcpy_s(c, “VisualC++”);
→コンパイルエラー

strcpy_s(c, ?, myStr);
コピー先のサイズが未定の為

char temp[50] = “VisualC++”;
char *c = temp;

strcpy_s(c, 50, “VisualC++”);
cout << c << ‘\n’;
→VisualC++
要素数が固定されているので○

指定の文字数分のみ複写
文字配列に対してのみ複写可能(ポインタ不可)
char before[] = “VisualC++”;
char after[10];

strncpy_s(after, before, 3);
cout << after << ‘\n’;
→Vis

strncpy_s(after, before, _countof(after)-1);
cout << after << ‘\n’;
→VisualC++

strncpy_s(after, before, 20);
→実行時エラー

ポインタに複写は不可
char *before = “VisualC++”;
char *after = ” “;
strncpy_s(after, before, 5);
→コンパイルエラー

char *before = “VisualC++”;
char temp[10];
char *after = temp;
strncpy_s(after, before, 5);
→コンパイルエラー

追加(結合)

string newStr = string.Concat(@”AAA”, @”BBB”, @”CCC”);
newStr:AAABBBCCC

int a = 123;
string b = @”BBB”;
string c = @”CCC”;
string str = string.Join(“,”, a, b, c);
str:123,BBB,CCC

char *before1 = “VisualC++”;
char *before2 = “VisualC#”;
char after[50];

strncpy_s(after, before1, _countof(after));
要素数が定義されている文字配列の末尾に追加
strcat_s(after, before2);

cout << after << ‘\n’;
→VisualC++VisualC#

strncpy_s(after, before1, 6);
要素数が定義されている文字配列の末尾に指定数文字数分追加
strncat_s(after, before2, 6);

cout << after << ‘\n’;
→VisualVisual

$s1 = “aaa”;
$s2 = “bbb”;
$s = $s1 . $s2;
$s .= “ccc”;
→ aaabbbccc

$before = [‘a’, ‘b’, ‘c’];
$after = implode(“,”, $before);
→’a,b,c’

s = ‘aaa’ + ‘bbb’ + ‘ccc’
print(s)
# aaabbbccc

s = ‘aaa’
s += ‘bbb’
s += ‘ccc’
print(s)
# aaabbbccc

s = ‘aaa’\
  ‘bbb’\
  ‘ccc’
print(s)
# aaabbbccc

print(1 + ‘1’) # エラー
print(str(1) + ‘1’) # 11

比較

String型は基本型ではなく、参照型。
(「プログラム言語 変数/データ型」参照)

string s1 = “TEST”;
string s2 = “TEST”;
“TEST” というリテラルがメモリ領域に作成され、
s1、s2ともそこを参照する。

// 参照値の比較
bool ret1 = (s1 == s2);
⇒ ret1 : true
// 文字列の比較
bool ret2 = s1.Equals(s2);
⇒ ret2 : true

string s3 = new string (“TEST”);
string s4 = new string (“TEST”);
“TEST” というリテラルが2つメモリ領域に作成され、
s1、s2それぞれ別のメモリ領域を参照する。

// 参照値の比較
bool ret3 = (s3 == s4);
⇒ ret3 : false

// 文字列の比較
bool ret4 = s3.Equals(s4);
⇒ ret4 : true

char myChar1[] = “VisualC++”;
char myChar2[] = “VisualC#”;

int result;
result = strcmp(myChar1, myChar2);
result:1

char myChar1[] = “VisualC++”;
char myChar2[] = “VisualC++”;

result = strcmp(myChar1, myChar2);
result:0

char *myChar1 = “VisualC++”;
char *myChar2 = “VisualC#”;

result = strcmp(myChar1, myChar2);
ポインタOK
result:1

指定文字数分の比較
result = strncmp(myChar1, myChar2, 6);
result:0

bool = x.Equals(y)
//⇒bool : false;

bool = x.Equals(x)
//⇒bool : true;

String s1 = “TEST”;
String s2 = “TEST”;
“TEST” というリテラルがメモリ領域に作成され、
s1、s2ともそこを参照する。

// 参照値の比較
Boolean ret1 = (s1 == s2);
⇒ ret1 : true
// 文字列の比較
Boolean ret2 = s1.equals(s2);
⇒ ret2 : true

String s3 = new String(“TEST”);
String s4 = new String(“TEST”);
“TEST” というリテラルが2つメモリ領域に作成され、
s1、s2それぞれ別のメモリ領域を参照する。

// 参照値の比較
Boolean ret3 = (s3 == s4);
⇒ ret3 : false
// 文字列の比較
Boolean ret4 = s3.equals(s4);
⇒ ret4 : true

String myStr = “abc”;

Boolean a = myStr.startsWith(“a”);
⇒true
Boolean b = myStr.startsWith(“b”);
⇒false
Boolean c = myStr.startsWith(“c”, 2);
⇒true

検索(判定・位置)

bool = “12345”.Contains(“12”);
bool:true

bool ret = “12345”.All(x => x == ‘x’);
ret : false

string s = “123456789”;
int index = s.indexOf(“1”);
⇒ index : 0

int index = s.indexOf(“2”);
⇒ index : 1

int index = s.indexOf(“0”);
⇒ index : -1

char *myChar1 = “VisualC++”;
char myChar2[] = “VisualC#”;
char *result;

検索した文字が最初に現れるアドレスを取得
result = strchr(myChar1, ‘C’);
ポインタOK。
result:C++

result = strchr(myChar2, ‘C’);
result:C#

検索した文字列が最初に現れるアドレスを取得
result = strstr(myChar1, “C”);
ポインタOK。
result:C++

result = strstr(myChar2, “C”);
result:C#

Dim s As String = “123456789”
Dim index As Integer = s.IndexOf(“1”)
⇒ index : 0

Dim index As Integer = s.IndexOf(“2”)
⇒ index : 1

Dim index As Integer = s.IndexOf(“0”)
⇒ index : -1

$pos = strpos(‘2018-01-01 09:10:10’, ‘ ‘);
if ($pos !== false) {
 echo $pos;
 →10
}

#含むか?の判定
print(‘a’ in ‘abcde’) #true
print(‘z’ in ‘abcde’) #true

#開始するか?の判定
print(‘abcde’.startswith(‘a’)) #true
print(‘abcde’.startswith(‘e’)) #false

#終了するか?の判定
print(‘abcde’.endswith(‘a’)) #false
print(‘abcde’.endswith(‘e’)) #true

#開始位置
print(‘abcabc’.find(‘a’)) #0(先頭位置)
print(‘abcabc’.find(‘e’)) #-1
print(‘abcabcabc’.rfind(‘a’)) #6(最終位置)
print(‘abcabcabc’.rfind(‘e’)) #-1
print(‘abcabc’.index(‘a’)) #0(先頭位置)
print(‘abcabc’.index(‘e’)) #エラー
print(‘abcabcabc’.rindex(‘a’)) #6(最終位置)
print(‘abcabcabc’.rindex(‘e’)) #エラー

isnumeric=str.isnumeric()
isdecimal=str.isdecimal()
isdigit=str.isdigit()
isalpha=str.isalpha()
isalnum=str.isalnum()
islower=str.islower()
isupper=str.isupper()

isnumeric isdecimal isdigit isalpha isalnum islower isupper
111 True True True False True False False
111 True True True False True False False
一壱 True False False True True False False
Aa False False False True True False False
aa False False False True True True False
AA False False False True True False True
Aa False False False True True False False
aa False False False True True True False
AA False False False True True False True
アア False False False True True False False
あ亜 False False False True True False False
#! False False False False False False False

var s = “123456789”;
var index = s.indexOf(“1”);
⇒ index : 0

var index = s.indexOf(“2”);
⇒ index : 1

var index = s.indexOf(“0”);
⇒ index : -1

置換

String before = “abcde”;
String after = before.Replace(oldValue:”c”, newValue:””);
⇒ after : abde
Dim before As String = “abcde”
Dim after As String = before.Replace(“c”,””)
⇒ after : abde
String myBefore = String.valueOf(“AAAAA”);

String myAfter = myBefore.replace(“AA”,”BB”);
⇒myAfter : BBBBA
String myAfter = myBefore.replace(‘AA’,’BB’);
⇒コンパイルエラー
String myAfter = myBefore.replace(“AA”,’B’);
⇒コンパイルエラー
String myAfter = myBefore.replace(‘A’,’B’);
⇒myAfter : BBBBB

正規表現を用いた置換(replaceAll)
置換後文字列 = 置換対象(置換前)文字列.replaceAll(“正規表現文字列”, “置換する文字列”);

String myStr = “AAAABBBBCCCC”;
String newStr = myStr.replaceAll(“.”, “Z”);
⇒newStr : ZZZZZZZZZZZZ

str_replace(検索文字列,置換文字列,検索対象)

$after = str_replace(“bbb”, “zzz”, “aaabbbccc”);
$after:”aaazzzccc”

検索語を配列で複数指定
$search = array(“aaa”,”bbb”,”ccc”);
$search = array(“\r\n”,”\r”,”\n”); で改行コードを指定可
$after = str_replace($search, “zzz”, “aaabbbccc”);
$after:”zzzzzzzzz”

$search = array(“aaa”, “bbb”, “ccc”);
$replace = array(“AAA”, “BBB”, “CCC”);
$after = str_replace($search, $replace, “aaabbbccc”);
$after:”AAABBBCCC”

$search = array(“aaa”, “bbb”, “ccc”);
$replace = array(“AAA”, “BBB”);
置換文字列が、検索対象より少ない
$after = str_replace($search, $replace, “aaabbbccc”);
$after:”AAABBB”
空文字で置換される

echo str_replace(“\r\n”, “<br>”, “~<br>~”)

~\r\n

【正規表現を用いた置換】
$ret = preg_replace( 検索, 置換, 対象);
$ret = preg_replace( ‘/正規表現/’, ~, ~);
$ret = preg_replace( ‘/(\s| )/’, ”, ‘ aa a ‘);
→aaa

【htmlへの置換】
XHTML
echo nl2br(“~\r\n~”);

~<br />

非XHTML
echo nl2br(“~\r\n~”, false);

~<br>

【…への置換】
$ret = $text->truncate(
 aaaaaaaaaa,
 5,
 array(
  ’ending’ => ‘…’
 )
);
$ret →
aaaaa…

【…への置換】
$ret = $this->Text->truncate(
 aaaaaaaaaa,
 5,
 array(
  ’ellipsis’ => ‘…’
 )
);
$ret →
aaaaa…
#全て置換
print(‘aaaaa’.replace(‘a’, ‘b’)) #bbbbb

#置換個数を指定
print(‘aaaaa’.replace(‘a’, ‘b’, 3)) #bbbaa

#正規表現で置換
import re

print(re.sub(r’^a’, ‘A’, ‘aaaaa’)) #Aaaaa

var val = 文字列.replace( 置換前, 置換後 );

alert( ‘1<br>2<br>3’ ).replace( ‘<br>’, ”);
→12<br>3
通常置換は1回だけ

alert( ‘1<br>2<br>3’ ).replace( /<br>/g, ”);
→123
正規表現利用により置換は最後まで可能

正規表現利用時
/ で囲む
末尾にg

chomp
改行削除
my $input = <STDIN>; ※5を入力したとする。
chomp $input;
$input *= 2;
print $input
⇒ $input : 10

windows等のテキストファイルの改行を削除する場合
$str =~ s/\r//; #CRを削除する
$str =~ s/\n//; #LFを削除する

トリム

前後半角スペーストリム
String s = “ A A A ”.trim();
s:”A A A”

全角スペーストリム
String s = StringUtils.strip(“ ”);
s:””

$ret = trim(‘ a aa  ‘);
$ret:a aa

型変換

プログラム言語 データ型:型情報」参照

大小文字変換

大文字変換
UCase(~)

小文字変換
LCase(~)

strBig = UCase(~)
strSmall = LCase(~)

String s = “Abc”;
System.out.println(s.toLowerCase()); →abc
System.out.println(s.toUpperCase()); →ABC

print(‘aaa’.upper()) # AAA
print(‘AAA’.lower()) # aaa

切り出し

const string CONST_STRING = “abcdefghijklmn”;

string str1 = CONST_STRING.Substring(3,3);
⇒str1:def

※× substring(開始位置, 終了位置)ではない。JAVAとは違う。
※○ substring(開始位置, 文字数)

Const CONST_STRRING As String = “abcdefghijklmn”

Dim str1 As String = Strings.Left(CONST_STRRING, 3)
⇒str1:abc
Dim str2 As String = Strings.Right(CONST_STRRING, 3)
⇒str2:lmn
Dim str3 As String = Strings.Mid(CONST_STRRING, 3, 3)
⇒str1:cde
Dim str4 As String = CONST_STRRING.Substring(startIndex:=3,length:=3)
⇒str1:def

String myBefore = String.valueOf(“ABCDEFG”);

String myAfter = myBefore.substring(3, 5);
⇒myAfter : DE
※× substring(開始位置, 文字数ではない) .Netとは違う
※○ substring(開始位置, 終了位置)

String myAfter = myBefore.substring(3, 2);
⇒実行時エラー

$year = substr(‘2018-01-01 09:10:10’, 0, 4);
$year:2018

print(‘abcde'[0]) #a(先頭)
print(‘abcde'[1]) #b
print(‘abcde'[4]) #e
print(‘abcde'[-1]) #e(末尾)
print(‘abcde'[0:2]) #先頭から2文字目
print(‘abcde'[:2]) #先頭から2文字目
print(‘abcde'[2:]) #3文字目から末尾
print(‘abcde'[:]) #先頭から末尾
print(‘abcde'[-3:]) #後ろから3文字目から末尾
print(‘abcde'[-3:-2]) #後ろから3文字目から後ろから2文字目
my $before = “abcde”;
my $after = substr($before,0,2);
⇒ $after : ab
“文字列”.substring(開始位置 , 終了位置)
開始位置は0スタート

var Value = “ABCDEFG”.substring( 1 , 3 ) ;
// ⇒ Value : BC

SET STR=12345

構文 意味 結果
%STR% 変数STRの値全体 12345
%STR:~2% 2文字目から、最後まで 345
%STR:~2,2% 2文字目から、2文字分 34
%STR:~2,-2% 2文字目から、最後の2文字分を除いたもの 3
%STR:~-2% 後ろから2文字目から、最後まで 45
%STR:~-3,2% 後ろから3文字目から、2文字分 34
%STR:~-3,-2% 後ろから3文字目から、最後の2文字分を除いたもの 3

文字数

char myChar1[] = “VisualC++”;
char *myChar2 = “VisualC#”;

int elmCnt;
elmCnt = strlen(myChar1);
elmCnt:9
終端文字(\0)は含まない

elmCnt = strlen(myChar2);
ポインタOK
elmCnt:8
終端文字(\0)は含まない

バイト数

int i = “ア”.getBytes(Charset.forName(“UTF-8”)).length;
→3
int i = “ア”.getBytes(Charset.forName(“Shift-JIS”)).length;
→2
int i = “ア”.getBytes(Charset.forName(“UTF-8”)).length;
→2
int i = “ア”.getBytes(Charset.forName(“Shift-JIS”)).length;
→1

int i = “ア”.getBytes().length;
→?
実行環境(Windows?Linux?)に影響される
Windows=Shift-Jis、Linux=UTF-8

Char型とString型

char[] myChar1 = new char[3];
myChar1[0] = ‘1’;
myChar1[1] = ‘2’;
myChar1[2] = ‘3’;
//myChar[3] = ‘4’; はエラー

char[] myChar2 = {‘1′,’2′,’3’};

char[] myChar3 = “123”.ToCharArray();

int len = myChar1.Length;
//⇒len:3
int number = (int)myChar1[0];
//⇒number:49(文字コード)
char myChar = (char)number;
//⇒myChar:’1′
string myString = myChar.ToString();
//⇒myString:”1″

Dim myChar1(3) As Char
myChar1(0) = “1”
myChar1(1) = “2”
myChar1(2) = “3”
myChar1(3) = “4”
‘myChar1(4) = “5” はインデックスエラー

Dim myChar2() As Char = {“1”, “2”, “3”}
Dim myChar3() As Char = “123”.ToCharArray()

Dim len As Integer = myChar1.Length
‘len:4
Dim number As Integer = Val(myChar1(0))
‘number:1
Dim myString As String = number.ToString()
‘myString:”1″

バインド(埋め込み)

{0}、{1}に対して値を指定する
string disp1 = string.Format(“aaaa{0}aaa{1}aaa{2}”, myChar1[0] , myChar1[1] , myChar1[2]);
⇒disp1 : “aaaa1aaa2aaa2”

{0}、{1}に対して値を指定する
Dim disp1 As String = String.Format(“aaaa{0}aaa{1}aaa{2}”, myChar1(0), myChar1(1), myChar1(2))

%sに対して値を指定する
String s = String.format(“000%s000%s000%s000″, “A”,”B”,”C”)

{N}に対して値を指定する
String s = MessageFormat.format(“000{0}000{1}000{2}000″,”A”,”B”,”C”);
String s = MessageFormat.format(“000{0}000{1}000{2}000″,new Object[]{“A”,”B”,”C”}});

S:000A000B000C000

purpose = “ppp”;

alert( `ccc${purpose}ccc` );
→ cccpppccc

$s = “XXXXX{$purpose}XXXXX”;

連想配列
$s = “XXXXX$array[name]XXXXX”;

オブジェクト
$s = “XXXXX{$item->name}XXXXX{$item->method()}“;

定数
define(“TEST_MESSAGE”, “テスト”);

定数を出力するためのクロージャ
$constant = function($c){ return $c; };

$s = “XXXXX{$constant(TEST_MESSAGE)}XXXXX”;

#format関数
print(‘aaa{}aaa{}aaa’.format(‘ddd’, ‘ddd’))
→aaadddaaadddaaa

print(‘aaa{0}aaa{1}aaa’.format(‘ddd’, ‘ddd’))
→aaadddaaadddaaa

print(‘aaa{a}aaa{b}aaa’.format(a=’ddd’, b=’ddd’))
→aaadddaaadddaaa

関数の戻り値をバインド
print(‘aaa{a}aaa{b}aaa’.format(a=getString(), b=getString()))
→aaa(getString()の戻り値)aaa(getString()の戻り値)aaa

配列
ary = [‘php’, ‘java’, ‘python’]
print(‘{0[0]}、{0[1]}、{0[2]}’.format(ary))

連想配列
ary = {‘php’:’CakePHP3′, ‘java’:’Spring-Boot’, ‘python’:’Django’}
print(‘{0[php]}、{0[java]}、{0[python]}’.format(ary))

#f文字への埋め込み
str1 = ‘ddd’
str2 = ‘ddd’
print(f’aaa{str1}aaa{str2}aaa’)
→aaadddaaadddaaa

print(f’aaa{getString()}aaa{getString()}aaa’)
→aaa(getString()の戻り値)aaa(getString()の戻り値)aaa

my $y = “bbb”;
my @z = (c,c,c);

#「”」で囲まれた文字列中の変数
print “aaa$y@z”;
⇒aaabbbccc

#「’」で囲まれた文字列中の変数
print ‘aaa$y@z’;
⇒aaa$y@z

0埋め

string disp2 = “55”.PadLeft(5, ‘0’);
disp2:55000
string disp3 = “55”.PadRight(5, ‘ ‘);
disp2:  55

通常の{0}、{1} に加え、書式:xxxxxを指定する
string disp4 = String.Format(“{0:00000}”, 55);
⇒disp4 : “00055”

Dim disp2 As String = “55”.PadLeft(5, CType(“0”, Char))
Dim disp3 As String = “55”.PadRight(5, CType(” “, Char))
‘通常の{0}、{1} に加え、書式:xxxxxを指定する
Dim disp4 As String = String.Format(“{0:00000}”, 55)
‘⇒disp4 : “00055”

s = String.format(“%05d”, 123);
→00123
s = String.format(“%5s”, 123);
→ 123
s = String.format(“%5s”, “123”).replace(” “, “0”);
→00123
s = String.format(“%,d”, 123567);
→1,234,567

sprintf(‘%05d’, 9)
→00009

sprintf(‘%5s’, 9)
→    9

print(‘python’.rjust(10)) # ____python
print(‘python’.rjust(10, ‘ ‘)) # ____python
print(‘python’.rjust(10, ‘0’)) # 0000python

print(‘python’.ljust(10)) # python____
print(‘python’.ljust(10, ‘ ‘)) # python____
print(‘python’.ljust(10, ‘0’)) # python0000

print(‘python’.center(10)) # __python__
print(‘python’.center(10, ‘ ‘)) # __python__
print(‘python’.center(10, ‘0’)) # 00python00

var value = 5
(“0” + value).slice(-2);
// ⇒ value : 05

小数点桁指定

//小数点桁指定
string disp5 = String.Format(“{0:0.00000}”, 55.123);
//⇒disp5 : “55.12300”

//小数点桁指定&0埋め
string disp6 = String.Format(“{0:000.00000}”, 12.345);
//⇒disp6 : “012.34500”

‘小数点桁指定
Dim disp5 As String = String.Format(“{0:0.00000}”, 55.123)
‘⇒55.12300

‘小数点桁指定&0埋め
Dim disp6 As String = String.Format(“{0:000.00000}”, 55.123)
‘⇒055.12300

var value = (0.33333).toFixed(2);
// ⇒ value : 0.33

桁区切り

//カンマ区切り
string disp7 = String.Format(“{0:#,#}”, 123456789);
//⇒disp7 : 123,456,789

//カンマ区切り&小数点桁指定
string disp8 = String.Format(“{0:#,#.00}”, 12345.6);
//⇒disp8 : 12,345.60

‘カンマ区切り
Dim disp7 As String = String.Format(“{0:#,#}”, 1234567890)
‘⇒1,234,567,890

‘カンマ区切り&小数点桁指定
Dim disp8 As String = String.Format(“{0:#,#.00}”, 12345.6)
‘⇒12,345.60

echo number_format(1234.567);
echo number_format(1234.567, 0);
→1,235
小数点以下四捨五入

echo number_format(1234.567, 2);
→1,234.57
小数点第三位を四捨五入

Null

Null判定
bool ret;

ret = string.IsNullOrWhiteSpace(value: @”AAA”);
ret:false
ret = string.IsNullOrWhiteSpace(value: @” “);
ret:true
ret = string.IsNullOrWhiteSpace(value: @””);
ret:true
ret = string.IsNullOrWhiteSpace(value: null);
ret:true

ret = string.IsNullOrEmpty(value: @”AAA”);
ret:false
ret = string.IsNullOrEmpty(value: @” “);
ret:false
ret = string.IsNullOrEmpty(value: @””);
ret:true
ret = string.IsNullOrEmpty(value: null);
ret:true

Objects.~
System.out.println(Objects.isNull(null)); // true
System.out.println(Objects.isNull(“Java”)); // false

System.out.println(Objects.nonNull(null)); // false
System.out.println(Objects.nonNull(“Java”)); // true

System.out.println(Objects.compare(null, null, Comparator.naturalOrder())); // 0
System.out.println(Objects.compare(“Java”, “Java”, Comparator.naturalOrder())); // 0
System.out.println(Objects.compare(“Java”, “PHP”, Comparator.naturalOrder())); // -~
System.out.println(Objects.compare(“Java”, null, Comparator.naturalOrder())); // 例外

Object obj = Objects.requireNonNull(“Java”); // obj:”Java”
Object obj = Objects.requireNonNull(null); // 例外
Object obj = Objects.requireNonNull(null, “nullです”); // 例外メッセージ

if ( !is_null(~) ) {
 nullでない時の処理
}

isset() empty() is_null() ===null ===”
1 数字 1 0 0 0 0
‘a’ 文字 1 0 0 0 0
null 0 1 1 1 0
1 1 0 0 1

null合体演算子(PHP7以降)
$result = $aaa ?? 1;

↓と同じ
$result = is_null($aaa) ? 1 : $aaa;

置換