文字列


  String Class

文字列は、アプリケーションにとって最も重要な要素です。これさえ扱えればとにかく前へ進めます。
Class としての文字列は C++ でも普通に実装されていますし、自分で実装する事もそう難しく無い
ので、C++ のクラスを作成した事があれば、比較的簡単に Java に頭を切り替える事が出来ます

  簡単な文字列への変換 ( 文字列連結演算子 + )

文字列変換は、実引数の一方が String のときに二項演算子 + のオペランドにだけ適用する

  

byte a = 1;
short b = 2;
int c = 3;
long d = 4;
float e = 5.999f;
double f = 6.999d;
boolean g = true;

String dest = "" + a + b + c + d + e + f + g; 
dest += a;

// 12345.9996.999true1 と表示される
System.out.println( dest );
  

  文字列から数値に変換

基本型への変換は、基本型に対応する数値クラスのメソッドを使用します

  

String strValue = "0";

byte a = Byte.parseByte(strValue);
short b = Short.parseShort(strValue);
int c = Integer.parseInt(strValue);
long d = Long.parseLong(strValue);
float e = Float.parseFloat(strValue);
double f = Double.parseDouble(strValue);
  

同様に、数値クラス型への変換も、各数値クラスのメソッドを使用します

  

String strValue = "0";

Byte a = Byte.valueOf(strValue);
Short b = Short.valueOf(strValue);
Integer c = Integer.valueOf(strValue);
Long d = Long.valueOf(strValue);
Float e = Float.valueOf(strValue);
Double f = Double.valueOf(strValue);
  

  等しい文字列

Java では オブジェクトに対する処理は殆どメソッドを使うと考えて良いです。よって、ある文字列が
別の文字列と一致するかどうかの判断も equals というメソッドを使用して判断します( 戻り値 boolean )

  

String strValue = "ABC";

if ( strValue.equals( "ABC" ) ) {
	System.out.println("等しい");
}

if ( !strValue.equals( "XYZ" ) ) {
	System.out.println("等しくない");
}
  

  現在の日付・時間

  

import java.util.*;
import java.text.*;

public class CLCOMP { public static void main(String[] args) {

	Date myDate = null;
	String strDate = null;

	myDate = new Date();
	strDate = DateFormat.getDateInstance().format(myDate);
	System.out.println( "現在の日付 : " + strDate );

	strDate = DateFormat.getTimeInstance().format(myDate);
	System.out.println( "現在の時間 : " + strDate );

	strDate = DateFormat.getDateTimeInstance().format(myDate);
	System.out.println( "現在の日付・時間 : " + strDate );
} }
  

  任意の日付・時間

  

import java.util.*;
import java.text.*;

public class CLCOMP { public static void main(String[] args) {

	Calendar cal = null;
	Date myDate = null;
	String strDate = null;

	cal = Calendar.getInstance();
	cal.set( 2006, 0, 14, 18, 31 ,10 );
	myDate = cal.getTime();

	strDate = DateFormat.getDateInstance().format(myDate);
	System.out.println( "現在の日付 : " + strDate );

	strDate = DateFormat.getTimeInstance().format(myDate);
	System.out.println( "現在の時間 : " + strDate );

	strDate = DateFormat.getDateTimeInstance().format(myDate);
	System.out.println( "現在の日付・時間 : " + strDate );

} }
  

  数値編集

  

import java.text.*;

public class CLCOMP { public static void main(String[] args) {

	double nValue1 = 0;
	long nValue2 = 0;
	DecimalFormat decFormat = null;

	nValue1 = 123456789.125;
	decFormat = new DecimalFormat(",##0.00");
	System.out.println(decFormat.format(nValue1));

	nValue1 = 123456789.126;
	decFormat = new DecimalFormat(",##0.00");
	System.out.println(decFormat.format(nValue1));

	nValue2 = 1234567890;
	decFormat = new DecimalFormat(",##0");
	System.out.println(decFormat.format(nValue2));

} }
  




yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[javaSwing]
claudebot
24/03/29 21:26:16
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube