YouTip LogoYouTip

Scala Strings

The following example assigns a string to a constant:\\n\\n## Example\\n\\nobject Test {\\n\\nval greeting: String ="Hello,World!"\\n\\ndef main(args: Array){\\n\\n println( greeting )\\n\\n}\\n\\n}\\n\\nThe above example defines the variable greeting as a string constant, and its type is String (java.lang.String).\\n\\nIn Scala, the type of a string is actually Java String, and it does not have a String class of its own.\\n\\nIn Scala, String is an immutable object, so the object cannot be modified. This means that if you modify the string, a new string object will be created.\\n\\nOther objects, however, such as arrays, are mutable objects. Next, we will introduce the commonly used java.lang.String methods.\\n\\n* * *\\n\\n## Creating a String\\n\\nCreating a string example is as follows:\\n\\nval greeting = "Hello World!";or val greeting:String = "Hello World!";\\nYou do not necessarily have to specify the String type for the string, because the Scala compiler will automatically infer the type of the string as String.\\n\\nOf course, we can also explicitly declare the string as a String type, as shown in the following example:\\n\\n## Example\\n\\nobject Test {\\n\\nval greeting: String ="Hello, World!"\\n\\ndef main(args: Array){\\n\\n println( greeting )\\n\\n}\\n\\n}\\n\\nExecute the above code, the output result is:\\n\\n$ scalac Test.scala $ scala TestHello, world!\\nAs we mentioned earlier, the String object is immutable. If you need to create a modifiable string, you can use the StringBuilder class, as shown in the following example:\\n\\n## Example\\n\\nobject Test {\\n\\ndef main(args: Array){\\n\\nval buf =new StringBuilder;\\n\\n buf +='a'\\n\\n buf ++="bcdef"\\n\\n println("buf is : " + buf.toString);\\n\\n}\\n\\n}\\n\\n[Run Example »](#)\\n\\nExecute the above code, the output result is:\\n\\n$ scalac Test.scala $ scala Test buf is : abcdef\\n\\n* * *\\n\\n## String Length\\n\\nWe can use the length() method to get the length of a string:\\n\\n## Example\\n\\nobject Test {\\n\\ndef main(args: Array){\\n\\nvar palindrome ="example.com";\\n\\nvar len = palindrome.length();\\n\\n println("String Length is : " + len );\\n\\n}\\n\\n}\\n\\nExecute the above code, the output result is:\\n\\n$ scalac Test.scala $ scala TestString Length is : 14\\n\\n* * *\\n\\n## String Concatenation\\n\\nThe String class uses the concat() method to concatenate two strings:\\n\\nstring1.concat(string2);\\nExample demonstration:\\n\\nscala> "Official Website: ".concat("example.com"); res0: String = Official Website: example.com\\nYou can also use the plus sign (+) to concatenate:\\n\\nscala> "Official Website: " + " example.com" res1: String = Official Website: example.com\\nLet's look at a complete example:\\n\\n## Example\\n\\nobject Test {\\n\\ndef main(args: Array){\\n\\nvar str1 ="Official Website:";\\n\\nvar str2 ="example.com";\\n\\nvar str3 ="'s Slogan is:";\\n\\nvar str4 ="";\\n\\n println( str1 + str2 );\\n\\n println( str3.concat(str4));\\n\\n}\\n\\n}\\n\\nExecute the above code, the output result is:\\n\\n$ scalac Test.scala $ scala TestOfficial Website: example.com 's Slogan is:\\n\\n* * *\\n\\n## Creating Formatted Strings\\n\\nIn the String class, you can use the printf() method to format and output a string. The String format() method can return a String object instead of a PrintStream object. The following example demonstrates the use of the printf() method:\\n\\n## Example\\n\\nobject Test {\\n\\ndef main(args: Array){\\n\\nvar floatVar =12.456\\n\\nvar intVar =2000\\n\\nvar stringVar ="!"\\n\\nvar fs = printf("Floating-point variable is " +\\n\\n"%f, Integer variable is %d, String is " +\\n\\n" %s", floatVar, intVar, stringVar)\\n\\n println(fs)\\n\\n}\\n\\n}\\n\\nExecute the above code, the output result is:\\n\\n$ scalac Test.scala $ scala TestFloating-point variable is 12.456000, Integer variable is 2000, String is !()\\n\\n* * *\\n\\n## String Methods\\n\\nThe following table lists the commonly used methods in java.lang.String, which you can use in Scala:\\n\\n| No. | Method and Description |\\n| --- | --- |\\n| 1 | **char charAt(int index)** Returns the character at the specified position |\\n| 2 | **int compareTo(Object o)** Compares the string to an object |\\n| 3 | **int compareTo(String anotherString)** Compares two strings lexicographically |\\n| 4 | **int compareToIgnoreCase(String str)** Compares two strings lexicographically, ignoring case |\\n| 5 | **String concat(String str)** Concatenates the specified string to the end of this string |\\n| 6 | **boolean contentEquals(StringBuffer sb)** Compares this string to the specified StringBuffer. |\\n| 7 | **static String copyValueOf(char[] data)** Returns a String representing the character sequence in the specified array |\\n| 8 | **static String copyValueOf(char[] data, int offset, int count)** Returns a String representing the character sequence in the specified array |\\n| 9 | **boolean endsWith(String suffix)** Tests if this string ends with the specified suffix |\\n| 10 | **boolean equals(Object anObject)** Compares this string to the specified object |\\n| 11 | **boolean equalsIgnoreCase(String anotherString)** Compares this String to another String, ignoring case |\\n| 12 | **byte getBytes()** Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array |\\n| 13 | **byte[] getBytes(String charsetName** Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array |\\n| 14 | **void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)** Copies characters from this string into the destination character array |\\n| 15 | **int hashCode()** Returns a hash code for this string |\\n| 16 | **int indexOf(int ch)** Returns the index within this string of the first occurrence of the specified character |\\n| 17 | **int indexOf(int ch, int fromIndex)** Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index |\\n| 18 | **int indexOf(String str)** Returns the index within this string of the first occurrence of the specified substring |\\n| 19 | **int indexOf(String str, int fromIndex)** Returns the index within this string of the first occurrence of the specified substring, starting at the specified index |\\n| 20 | **String intern()** Returns a canonical representation for the string object |\\n| 21 | **int lastIndexOf(int ch)** Returns the index within this string of the last occurrence of the specified character |\\n| 22 | **int lastIndexOf(int ch, int fromIndex)** Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index |\\n| 23 | **int lastIndexOf(String str)** Returns the index within this string of the rightmost occurrence of the specified substring |\\n| 24 | **int lastIndexOf(String str, int fromIndex)** Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index |\\n| 25 | **int length()** Returns the length of this string |\\n| 26 | **boolean matches(String regex)** Tells whether or not this string matches the given regular expression |\\n| 27 | **boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)** Tests if two string regions are equal |\\n| 28 | **boolean regionMatches(int toffset, String other, int ooffset, int len)** Tests if two string regions are equal |\\n| 29 | **String replace(char oldChar, char newChar)** Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar |\\n| 30 | **String replaceAll(String regex, String replacement** Replaces each substring of this string that matches the given regular expression with the given replacement |\\n| 31 | **String replaceFirst(String regex, String replacement)** Replaces the first substring of this string that matches the given regular expression with the given replacement |\\n| 32 | **String[] split(String regex)** Splits this string around matches of the given regular expression |\\n| 33 | **String[] split(String regex, int limit)** Splits this string around matches of the given regular expression |\\n| 34 | **boolean startsWith(String prefix)** Tests if this string starts with the specified prefix |\\n| 35 | **boolean startsWith(String prefix, int toffset)** Tests if the substring of this string beginning at the specified index starts with the specified prefix. |\\n| 36 | **CharSequence subSequence(int beginIndex, int endIndex)** Returns a new character sequence that is a subsequence of this sequence |\\n| 37 | **String substring(int beginIndex)** Returns a new string that is a substring of this string |\\n| 38 | **String substring(int beginIndex, int endIndex)** Returns a new string that is a substring of this string |\\n| 39 | **char[] toCharArray()** Converts this string to a new character array |\\n| 40 | **String toLowerCase()** Converts all of the characters in this String to lower case using the rules of the default locale |\\n| 41 | **String
← Scala ArraysScala Closures β†’