SlideShare a Scribd company logo
1 of 12
Scalaの文字列処理
Day 4 Stringの文字コード変換と数値型との相互変換
Charset(文字コード)の
正式名称(Canonical Name)とエイリアス
• 正式名称「windows-31j」のエイリアスは「MS932
」など
• 正式名称「Shift-JIS」のエイリアスは「shift-jis」や
「sjis」など
Charsetチートシート
• デフォルト文字コード取得 Charset.defaultCharset()
• 正式名称やエイリアスからの文字コード取得
Charset.forName(“MS932")
• StandardCharsetsからUnicode系の文字コードを取得
StandardCharsets.UTF_8
• 文字コードの正式名称の取得 charset.name()
• 利用可能な文字コードの正式名称の取得 Charset.availableCharsets()
• 利用可能な文字コードのエイリアスの取得 charset.aliases()
文字コードの変換
Stringのコンストラクタで文字コード変換可能
(1)UTF-16BEからEUC-JPに符号化しUTF_8で復号化:
new String(str.getBytes(“EUC-JP”), StandardCharsets.UTF_8)
(2)UTF-16BEをwindows-31jで復号化:
new String(str.getBytes(), “MS932”)
Javaのプリミティブ型と
Stringとの相互変換は面倒
Javaは次のようなことを考慮するため面倒臭いですが、Scalaをやる人は読まなくていいです。
• widening primitive conversion
• narrowing primitive conversion
• primitive wrapper class
• auto-boxing conversion (valueOf)
• auto-unboxing conversion (booleanValue, charValue, etc.)
• String.valueOf and primitive wrapper class’s toString
• primitive wrapper class’s parse method (parseBoolean, parseInteger, etc.)
• CharBuffer.wrap(charArray).toString (<- kore wa slow dakara iranai kedo mizumashi tezuka osamu
mitai ni romaji de kaite mita)
Scalaの場合、
プリミティブ型がないので楽
• Intなど数値からStringへの変換はtoStringメソッド
• StringからIntなど数値への変換はtoIntメソッド(
NumberFormatExceptionに注意)
java.lang.Boolean.
parseBoolean
Javaのboolean型のラッパークラスのBooleanの
parseBooleanメソッドは、文字のケース(大文字、小
文字など)を無視して”true”の場合はtrue、それ以外は
全てfalseを返す。
なお、StringのtoBooleanメソッドは文字のケースを無
視し”false”の場合のみfalse、”true”でも”false”でもない
場合はIllegalArgumentExceptionを返す。
特定の進数表記
Javaのプリミティブ型のラッパークラスには特定の
進数表記に変換するメソッドが用意されている。
2進数 8進数 16進数
java.lang.Integer toBinaryString toOctalString toHexString
java.lang.Long toBinaryString toOctalString toHexString
java.lang.Float toHexString
java.lang.Double toHexString
任意の進数表記
Javaのプリミティブ型のラッパークラスのメソッドによる
基数(radix)が任意のNであるN進数表記の相互変換
Stringへの変換 Stringからの変換
java.lang.Byte parseByte(str, N)
java.lang.Short parseShort(str, N)
java.lang.Integer toString(value, N)
parseInt(str, N)
parseUnsignedInt(str, N)
java.lang.Long toString(value, N)
parseLong(str, N)
parseUnsignedLong(str, N)
文字とN進数表記での
数値の相互変換
• Character.numericValueは文字のN進数表記での値を取得
(例:’G’は16)
• Character.digitは文字のN進数表記での値を取得、N進数表
記に含まれない文字は-1を返す(例:16進数の場合、’G’は
存在しないので-1)
• Character.forDigitは引数で与えた数値をN進数で表現され
る文字(小文字)に変換(例:10は16進数で’a’)
Pimp My Libraryパターン
object FloatUtils {
implicit def floatToFloatUtils(repr: Float): FloatUtils = {
new FloatUtils(repr)
}
}
class FloatUtils(repr: Float) {
def toHexString: String = {
java.lang.Float.toHexString(repr)
}
}
Enrich My Libraryパターン
implicit class FloatUtils(repr: Float) {
def toHexString: String = {
java.lang.Float.toHexString(repr)
}
}

More Related Content

Recently uploaded

Recently uploaded (7)

業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
 
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
 
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
 
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Scalaの文字列処理 Day 4 Stringの文字コード変換と数値型との相互変換