14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

文字列からあ行など取得する

Last updated at Posted at 2014-02-23

仕様

  • 英語は全角でも半角でも半角に
  • カタカナもひらがなに
  • 記号などはブランク

ソース

objective-c
- (NSString *)getJapaneseLine:(NSString *)aString
{
    if (aString.length == 0) {
        return @"";
    }

    NSString *uppercaseString = [aString uppercaseString];
    NSMutableString *halfString = [uppercaseString mutableCopy];
    CFStringTransform((CFMutableStringRef)halfString, NULL, kCFStringTransformFullwidthHalfwidth, FALSE);
    NSMutableString *kanaString = [halfString mutableCopy];
    CFStringTransform((CFMutableStringRef)kanaString, NULL, kCFStringTransformHiraganaKatakana, TRUE);
    unichar uc = [kanaString characterAtIndex:0];
    
    if (uc >= 0x3041 && uc <= 0x304A) {
        return @"あ";
    } else if (uc >= 0x304B && uc <= 0x3054) {
        return @"か";
    } else if (uc >= 0x3055 && uc <= 0x305E) {
        return @"さ";
    } else if (uc >= 0x305F && uc <= 0x3069) {
        return @"た";
    } else if (uc >= 0x306A && uc <= 0x306E) {
        return @"な";
    } else if (uc >= 0x306F && uc <= 0x307D) {
        return @"は";
    } else if (uc >= 0x307E && uc <= 0x3082) {
        return @"ま";
    } else if (uc >= 0x3083 && uc <= 0x3088) {
        return @"や";
    } else if (uc >= 0x3089 && uc <= 0x308D) {
        return @"ら";
    } else if (uc >= 0x308F && uc <= 0x3093) {
        return @"わ";
    } else if (uc >= 0x0041 && uc <= 0x005A) {
        // A-Z
        return [NSString stringWithFormat:@"%C", uc];
    } else {
        // その他
        return @"";
    }
}
swift
func getJapaneseLine(aString: String) -> String {
	if line.isEmpty {
		return "";
	}
	
	var keyString = String(aString[aString.startIndex]).uppercaseString
	var halfString = NSMutableString(string: keyString) as CFMutableString
	CFStringTransform(halfString, nil, kCFStringTransformFullwidthHalfwidth, false)
	var kanaString = NSMutableString(string: halfString) as CFMutableString
	CFStringTransform(kanaString, nil, kCFStringTransformHiraganaKatakana, true)
	keyString = kanaString as String
	let c = first(keyString.unicodeScalars)!.value

	if c >= 0x3041 && c <= 0x304A {
		return "あ"
	} else if c >= 0x304B && c <= 0x3054 {
		return "か"
	} else if c >= 0x3055 && c <= 0x305E {
		return "さ"
	} else if c >= 0x305F && c <= 0x3069 {
		return "た"
	} else if c >= 0x306A && c <= 0x306E {
		return "な"
	} else if c >= 0x306F && c <= 0x307D {
		return "は"
	} else if c >= 0x307E && c <= 0x3082 {
		return "ま"
	} else if c >= 0x3083 && c <= 0x3088 {
		return "や"
	} else if c >= 0x3089 && c <= 0x308D {
		return "ら"
	} else if c >= 0x308F && c <= 0x3093 {
		return "わ"
	} else if c >= 0x0041 && c <= 0x005A {
		// A-Z
		return String(format: "%C", c)
	} else {
		// その他
		return "";
	}
}
14
14
0

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
14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?