LoginSignup
1
4

More than 5 years have passed since last update.

各言語のよく使う構文覚書

Last updated at Posted at 2018-06-02

はじめに

すぐ忘れるので。少しずつ書き溜めていくつもりです。
扱っているのは C#, Java, Scala, JavaScript, Python3, PHP です。
Scala 初心者のため、どうせならまとめてみようと思ったのが切欠です。

サンプルへのリンク先はJavaScriptが『JSFiddle』で、他は『Ideone』を利用。
環境構築はこちらを参考に:各言語の環境構築と簡易確認と腕試し - Qiita

入出力

Hello World

C#
using System;
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello " + Console.ReadLine() + "!");
    }
}
Java
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Hello " + sc.next() + "!");
    }
}
Scala
object Main extends App {
    println("Hello " + readLine + "!");
}
JavaScript
function Main(input) {
  alert('Hello ' + input + '!');
}
Main('World');
Python3
print("Hello " + input() + "!")
PHP
<?php
fscanf(STDIN, "%s", $s);
echo "Hello " . $s . "!";

配列操作

初期化

C#
int[] ary1 = {1, 2, 3};
int[,] ary2 = {{11, 12, 13}, {21, 22, 23}, {31, 32, 33}};
int[][] ary3 = {new int[]{11, 12, 13}, new int[]{21, 22, 23}, new int[]{31, 32, 33}};
Java
int ary1[] = {1, 2, 3};
int ary2[][] = {{11, 12, 13}, {21, 22, 23}, {31, 32, 33}};
Scala
var ary1 = Seq(1, 2, 3);
var ary2 = Seq(Seq(11, 12, 13), Seq(21, 22, 23), Seq(31, 32, 33));
JavaScript
var ary1 = [1, 2, 3];
var ary2 = [[11, 12, 13], [21, 22, 23], [31, 32, 33]];
Python3
ary1 = [1, 2, 3]
ary2 = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]
PHP
$ary1 = [1, 2, 3];
$ary2 = [[11, 12, 13], [21, 22, 23], [31, 32, 33]];

ファイル操作

入出力

  • C#:ファイル入出力
C#
using System.IO;
using System.Text;

var text = File.ReadAllText(@"C:\hoge1.txt", Encoding.UTF8);
var lines = File.ReadAllLines(@"C:\hoge1.txt", Encoding.UTF8);

File.WriteAllText(@"C:\hoge2.txt", "ほげ", Encoding.UTF8);
File.WriteAllLines(@"C:\hoge2.txt", lines, Encoding.UTF8);
  • Java:ファイル入出力
Java
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;

List<String> lines = Files.readAllLines(Paths.get("C:\\hoge1.txt"), StandardCharsets.UTF_8);

Files.write(Paths.get("c:\\hoge2.txt"), lines, StandardCharsets.UTF_8);
Files.write(Paths.get("c:\\hoge3.txt"), "ほげ".getBytes(StandardCharsets.UTF_8));
  • Scala:ファイル入出力

今の所Javaと同じ方法以外でより良い方法は知らない。

色々と見よう見まね。

html
<form>
  <input type="file" id="file"/><br>
  <textarea id="write"></textarea><br>
  <a id="download" href="#" download="hoge.txt">ダウンロード</a>
</form>
JavaScript
document.getElementById("file").addEventListener('change', f => {
  var reader = new FileReader();
  reader.readAsText(f.target.files[0]);
  reader.addEventListener( 'load', () => {
    document.getElementById("write").value = reader.result;
  });
});
document.getElementById("download").addEventListener('click', () => {
  var text = document.getElementById("write").value;
  var blob = new Blob([ text ], { "type" : "text/plain" });
  document.getElementById("download").href = window.URL.createObjectURL(blob);
});
  • Python3:ファイル入出力
Python3
text = open('C:\hoge1.txt', 'r', encoding='utf-8').read()
open('C:\hoge2.txt', 'w', encoding='utf-8').write('ほげ')
  • PHP:ファイル入出力
PHP
$text = file_get_contents('C:\hoge1.txt');
file_put_contents('C:\hoge2.txt', 'ほげ');

随時追加

結構面倒なので、少しずつ追加していこう・・・

C#
Java
Scala
JavaScript
Python3
PHP
1
4
2

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
1
4