Scala string



1. String 기본


var string1 = "I am a string"
var string2 = "I'm d string"
var string3 = "Put \" quotes\" by escaping"
var string4 = "Special \t characters \n insert"

var string5 = """
Hello
String
World!
"""
var myLanguage = "Scala"
var myExperience = 7
var myString = s"I love $myLanguage"
import java.time.Year
val thisYear = Year.now.getValue

var myLanguage = "Scala"
var myExperience - 7
var myString1 = s"I love $myLanguage."
var myString2 = s"I started learning it in ${thisYear - myExperience}"
var numQ = 56
var numC = 53
var resultStr = f"""You got $numC questions out of
                $numQ correct. Your score was
                ${numC.toFloat/numQ.toFloat*100}%.2f%%"""
var string5 = "Use \n to print nuw lines"
var string6 = "Use \\n to print nuw lines"
var jsonString1 = "{\"key1\": \"value1\"}"
var jsonString2 = """
{"key1": "value1",
"key2":"value2",
"key3":"value3"}
"""

var jsonString3 = 
"""
    |{"key1":"value1",
    | "key2":"value2",
    | "key3":"value3"}
""".stripMargin

import java.time.Year

val thisYear = Year.now.getValue

var myLanguage = "Scala"
var myExperience = 7
var myString1 = s"I love $myLanguage."
var myString2 = s"I started learning it in ${thisYear - myExperience}"

var numQuestions = 56
var numCorrect = 53
var resultStr = f"""You got $numCorrect questions out of
                $numQuestions correct. Your score was
                ${numCorrect.toFloat/numQuestions.toFloat*100}%.2f%%"""
                
var string5 = raw"You cat print a new line with \n"
</thead> </table>

# 2. String Utility
```scala // String.length // String.foreach(operation) // String.toUpperCase() // String.toLowerCase() //String.charAt(index) // String.concat(anotherString) // String.trim // String.equals(anotherString) // 문자열 까지만 비교 // String.eq(anotherString) // point 까지 비교 // String.split("separator") ``` ```scala // split var string6 = "I really enjoyed the movie Parasite, this year's Academy Award winner" val arr1 = string6.split(' ') val arr2 = "Directly,form,a,sting,literal".split(',') ``` ```scala // Starts or Ends With // "someStringLiteral".startsWith("prefix", index) "someStringLiteral".startsWith("Str", 4) "someStringLiteral".startsWith("some") // "someStringLiteral".endsWith("suffix") "someStringLiteral".endsWith("al") //"someStringLiteral".indexOf("character") //"someStringLiteral".indexOf("character", index) index 부터 찾는다. //"someStringLiteral".indexOf("prefix") //"someStringLiteral".indexOf("prefix", index) "someStringLiteral".indexOf("S") "someStringLiteral".indexOf("S", 5) "someStringLiteral".indexOf("Li") "someStringLiteral".indexOf("NO") // lastIndexOf() 맨 마지막에 있는 것 // "someStringLiteral".substring(sIndex) non inclusive // "someStringLiteral".substring(sIndex, endIndex) "someStringLiteral".substring(3) "someStringLiteral".substring(3, 7) ```

# 3. Regula Expressions
구분 설명
%c character
%d decimal (integer) number (base 10)
%e exponential floating-point number
%f floating-point number
%i integer (base 10)
%o octal number (base 8)
%s a string of characters
%u unsigned decimal (integer) number
%x unmber in hexadecimal (base 16)
%% print a percent sign
\% print a percent sign
</tobdy> </table>
```scala // 2016-02-14 // raw"(\d{4})-(\d{2})-(\d{2})".r // "(S|s)cala.r or "[Ss]cala // xxxx or xxx-xxxx-xxxx // "[0-9]{3}[- ][0-9]{4}[- ][0-9]{4}".r // ERRxxxx // raw"ERR-(\d+)".r // regularExpression.findFirstIn(stringToSearch) "Hello".r.findFirstIn("Hello World") // regular Expression.findAllIn(stringToSearch) // someString.replaceFirst("srchStr", "replaceStr") "Hello World".replaceFirst("World", "Universe") // someString.replaceAll("srchStr", "replaceStr") // someString.replaceFistIn("srchStr", "replaceStr") "World".r.replaceFirstIn("Hello World", "Universe") // RegEx.replaceAllIn("srchStr", "replaceStr") // stringToMatch.equals(otherString) "Hello World".equals("Hello World") //stringToMatch.equalsIgnoreCase(otherString) // stringToMatch.matches(regularExpressionString) "Hello World".matches("H[a-z]* W[a-z]*") "A Scala String".length "A Scala String".toUpperCase "A Scala String".toLowerCase() "A Scala String".foreach(println) val scalaStringLiteral = "A scala String" val nulString = null val scalaString1 = new String("A Scala String") val scalaString2 = new String("A Scala String") scalaStringLiteral.eq("A Scala String") val string6 = "I really enjoyed the movie Parasite, this year's academy award winner" val arr1 = string6.split(' ') val arr2 = "Directly,from,a,string,literal".split(',') val result = "SKC&C Learning Scala".startsWith("SK") // Regular Expression val pattern1 = """Scala \d{1}\.\d{1,2}""".r val seq1 = "We are learning Scala 2.12" pattern1.findFirstIn(seq1) // Regular Expression val pattern1 = raw"ERR-(\d+)".r val seq1 = "ERR-39763" pattern1.findFirstIn(seq1) // Regular Expression val datePattern = """(\d{4})-(\d{2})-(\d{2})""".r val seq1 = "2022-11-09" datePattern.findFirstIn(seq1) seq1 match { case datePattern(year, month, day) => s"$year was goot for wind" } // Regular Expression val pattern1 = "[1-5]+".r val seq1 = "12 67 93 43 51" val result = pattern1.findAllIn(seq1) result.foreach(println) //pattern1: scala.util.matching.Regex = [1-5]+ //seq1: String = 12 67 93 43 51 //result: scala.util.matching.Regex.MatchIterator = non-empty iterator //12 //3 //43 //51 // Regular Expression "Hello World and World".replaceFirst("World", "Universe") // res162: String = Hello Universe and World // Regular Expression "Hello World and World".replaceAll("World", "Universe") // res162: String = Hello Universe and Universe ```

# 4.
Pattern Matches
abc... Letters
123... Digits
\d Any digit
\D Any non-digit character
. Any character
\. Literal period
[abc] a, b, pr c
[^abc] NOT a, b, nor c
[a-z] Characters a to z
\w Any alphanumeric character
\W Any non-alphanumeric character
{m} m repetitions
* Zero or more repetitions </tr>
+ One or more repetitions
? Optional character
\s Any whitespace character
\S Any non-whitespace character
(...) Capture group </tr>
(abc|def) abc or def