Javaで全角か半角の判定をしてみる.
�.2バイトかそれ以外かによる判定
全角:バイト数が2バイトの文字
半角:バイト数が1バイトの文字
この違いを活かした判別方法.
コード
private void method1(String s) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (String.valueOf(chars[i]).getBytes().length < 2) {
System.out.print("半");
} else {
System.out.print("全");
}
}
}
参考サイト
◇JAVA Servlet での全角文字判定
�.Unicodeで判定する
Javaにおいては文字列は全てUNICODEで表現される
⇒ 全角半角の判定も各文字のUNICODEで判断する
Unicode表 Unicode(ユニコード)一覧検索 & 逆引き検索「うにこ〜ど2」 を参照
コード
private void method2(String s) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if ((c <= '\u007e') || // 英数字
(c == '\u00a5') || // \記号
(c == '\u203e') || // ~記号
(c >= '\uff61' && c <= '\uff9f') // 半角カナ
) {
System.out.print("半");
} else {
System.out.print("全");
}
}
}
備考
識別する文字の前後の含みも入れている
参考サイト
◇JavaのString中の全角半角の判定
�.全部全角の文字列かそれ以外かの判定
�.の方法と同じで,文字列全体を検査しているところが違う
コード
private boolean method3(String s) {
byte[] bytes = s.getBytes();
if (s.length() != bytes.length) {
// 半角混入
return true;
} else {
// 全角のみ
return false;
}
}
参考サイト
◇Java String 全角半角を含む文字列の扱い
�.URLEncoderクラス
URLEncoderクラスを利用した判定方法.
コード
@SuppressWarnings("deprecation")
private void method4(String s) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
String temp = String.valueOf(chars[i]);
if (URLEncoder.encode(temp).length() < 4) {
System.out.print("半");
} else {
System.out.print("全");
}
}
}
備考
「メソッド encode(String) は型 URLEncoder で使用すべきではありません」ってでるけど問題なし
参考サイト
◇プログラマメモ2
これらを比較してみる
上に挙げた4つの動作が同じであるか「"全角han漢字\\~〜カナ"」という引数を与えて確かめてみた.
結果は
全全半半半全全半半全全全 // �
全全半半半全全半半全半半 // �
true // �
全全半半半全全半半全全全 // �
であった.
方法�.と�.では半角カナを判別できなかった.
これより,方法�が最も精度がいいといえる.
全方法の比較ソース
package isZenHan;
import java.net.URLEncoder;
public class isZenHan {
public static void main(String[] args) {
isZenHan z = new isZenHan();
z.method1("全角han漢字\\~〜カナ");
System.out.println();
z.method2("全角han漢字\\~〜カナ");
System.out.println(z.method3("全角han漢字\\~〜カナ"));
System.out.println();
z.method4("全角han漢字\\~〜カナ");
}
// 全全半半半全全半半全全全
private void method1(String s) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (String.valueOf(chars[i]).getBytes().length < 2) {
System.out.print("半");
} else {
System.out.print("全");
}
}
}
// 全全半半半全全半半全半半
private void method2(String s) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if ((c <= '\u007e') || // 英数字
(c == '\u00a5') || // \記号
(c == '\u203e') || // ~記号
(c >= '\uff61' && c <= '\uff9f') // 半角カナ
) {
System.out.print("半");
} else {
System.out.print("全");
}
}
}
// true
private boolean method3(String s) {
byte[] bytes = s.getBytes();
if (s.length() != bytes.length) {
// 半角混入
return true;
} else {
// 全角のみ
return false;
}
}
// 全全半半半全全半半全全全
@SuppressWarnings("deprecation")
private void method4(String s) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
String temp = String.valueOf(chars[i]);
if (URLEncoder.encode(temp).length() < 4) {
System.out.print("半");
} else {
System.out.print("全");
}
}
}
}
2012年8月20日月曜日
2012年8月11日土曜日
Microsoft Picks Another Web Standards Fight
"WebRTC is a way to allow browsers to get in touch with one another using audio or video data without the help of a server. Google has been something of a pioneer in this area, and submitted a suggested technology for the standard. Mozilla has gone along with it, making it all look good. Microsoft, on the other hand, just seemed to be standing on the sidelines, watching what was happening. However, Microsoft now has a product that needs something like WebRTC; namely, Skype. It has been working on a web-based version of Skype and this has focused the collective mind on the problems of browser-to-browser communication. It now agrees that a standard is needed, just not the one Google and Mozilla are behind. Microsoft has submitted its own proposals for CU-RTC-Web or Customizable, Ubiquitous Real Time Communication over the Web, to the W3C. It may well be that Microsoft's alternative has features that make it superior, but a single standard is preferable to a better non-standard. Given Microsoft's need to make Skype work in the browser, it seems likely that, should its proposal not be accepted as the standard, it will press on regardless, thus splitting the development environment. Both Google and Mozilla have already put a lot of work into WebRTC, and there are partial implementations in Firefox, Chrome and Opera."
登録:
投稿 (Atom)