Here, it returns -1 because it couldn’t find ‘n’. This is like lastIndexOf, except that it begins searching backwards(right to left), starting at the index we specify. Unlike indexOf, this Scala String Method returns the index of the last occurrence of the character we specify. Intern returns the canonical representation for the string object. Like the other version of indexOf for a single character, this begins searching at the index we specify. This Scala String Method returns the index of the first occurrence of the substring we specify, in the string. This is like indexOf, except that it begins searching at the index we specify. This Scala Sring returns the index of the first occurrence of the character ch in the string. This int hashCode method returns a hash code for the string. dstBegin denotes the index to begin at in the destination character array. scrBegin denotes the index to begin at in the source string, and srcEnd denotes the index to end at in the source string. This Scala String Method copies characters from the string into the destination character array. void getChars(int srcBegin, int srcEnd, char dst, int dstBegin) Java.io.UnsupportedEncodingException: UnionĪt (Unknown Source)Īt (Unknown Source) However, providing an invalid charset name will result in an error: scala> "Ayushi".getBytes("Union") With a named charset as a parameter, getBytes will use that charset to encode the string. It uses the platform’s default charset for this. GetBytes Scala String Method encodes a string into a sequence of bytes and stores it into a new byte array. scala> "Ayushi".equalsIgnoreCase("aYushi") This is like equals(), except that it ignores case differences. Boolean equalsIgnoreCase(String anotherString) This Scala String Method returns true if the string and the object are equal otherwise, false. This Scala String Method returns true if the string ends with the suffix specified otherwise, false. If equal, it returns true otherwise, false. Boolean contentEquals(StringBuffer sb)ĬontentEquals compares a string to a StringBuffer’s contents. We saw this when we talked about strings briefly. This will concatenate the string in the parameter to the end of the string on which we call it. Scala> "Ayushi".compareToIgnoreCase("aYushi") Scala> "Ayushi".compareToIgnoreCase("ayushi ") scala> "Ayushi".compareToIgnoreCase("ayushi") Int compareToIgnoreCase Scala String Method compares two strings lexicographically, while ignoring the case differences. scala> "Ayushi".compareTo("Ayushi ")Ĭomparing to a different type raises a type-mismatch error. Otherwise, it returns the difference between the two(the number of characters less in the shorter string, or the maximum ASCII difference between the two). This is like the previous one, except that it compares two strings lexicographically. This Scala String Method compares a string to another object. Isn’t it so much like Java? scala> "Ayushi".charAt(1) This method returns the character at the index we pass to it. This new technique is useful writing more readable code, safe and allows to extend and combine existing functionality.List of String Method in Scala with Example 1. Which is a nice combination of new Scala 2.10 features itself. Will be rewritten by compiler to new LogInterpolator(new StringContext("", " ", "")).log(logString, 2) String interpolators can also take arbitrary expressions. ![]() You’ve already seen an example here: val pi = 3.14 Within the String, we can access variables, object fields, functions calls, etc. Prepending s to any string literal allows the usage of variables directly in the string. Scala provides three string interpolation methods out of the box: s, f and raw. Starting in Scala 2.10.0, Scala offers a new mechanism to create strings from your data: String Interpolation. Println("value of pi = "+pi) // value of pi = 3.14 Println(s"value of pi = $pi") // value of pi = 3.14Īnd, here’s an example which does not uses any string interpolator. String Interpolation allows users to embed variable references directly in processed string literals. String Interpolation refers to substitution of defined variables or expressions in a given String with respected values.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |