xxxxxxxxxx
class Main
{
// Iterate over the characters of a string
public static void main(String[] args)
{
String s = "Techie Delight";
String[] arr = s.split("");
for (String ch: arr) {
System.out.print(ch);
}
}
}
xxxxxxxxxx
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.split(''));
console.log(str.split(' '));
console.log(str.split('ox'));
> Array ["T", "h", "e", " ", "q", "u", "i", "c", "k", " ", "b", "r", "o", "w", "n", " ", "f", "o", "x", " ", "j", "u", "m", "p", "s", " ", "o", "v", "e", "r", " ", "t", "h", "e", " ", "l", "a", "z", "y", " ", "d", "o", "g", "."]
> Array ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]
> Array ["The quick brown f", " jumps over the lazy dog."]
xxxxxxxxxx
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ');
console.log(words[3]);
// expected output: "fox"
const chars = str.split('');
console.log(chars[8]);
// expected output: "k"
const strCopy = str.split();
console.log(strCopy);
// expected output: Array ["The quick brown fox jumps over the lazy dog."]
xxxxxxxxxx
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.split(''));
console.log(str.split(' '));
console.log(str.split('ox'));
> Array ["T", "h", "e", " ", "q", "u", "i", "c", "k", " ", "b", "r", "o", "w", "n", " ", "f", "o", "x", " ", "j", "u", "m", "p", "s", " ", "o", "v", "e", "r", " ", "t", "h", "e", " ", "l", "a", "z", "y", " ", "d", "o", "g", "."]
> Array ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]
> Array ["The quick brown f", " jumps over the lazy dog."]
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ');
console.log(words[3]);
// expected output: "fox"
const chars = str.split('');
console.log(chars[8]);
// expected output: "k"
const strCopy = str.split();
xxxxxxxxxx
// Split a string with a regex for whitespace
String[] resultRegex = inputStr.split("\\s");
xxxxxxxxxx
Console.WriteLine("Enter in an input");
//store user input
string input = Console.ReadLine();
//store each word in the strArr
string[] strArr = input.Split('');
//returns the length of the split words
int freq = input.Split('').Length - 1;
Console.WriteLine("Input text");
string input = Console.ReadLine();
int freq;
//stores the char
char ch;
foreach (char c in input)
{
ch = c;
//stores number of times the letter occurs
freq = input.Split(ch).Length - 1;
Console.WriteLine(freq);
}
xxxxxxxxxx
>>> score1,score2 = input("Enter two scores separated by a comma:").split(",")
Enter two scores separated by a comma:1,2
>>> score1
'1'
>>> score1 = int(score1)
>>> score1
1
>>> score1 = float(score1)
>>> score1
1.0
>>>
xxxxxxxxxx
#include <string.h>
#include <stdio.h>
int main() {
char str[] = "hello";
int i;
int strLength = strlen(str);
for (i = 0; i < strLength; i++) {
printf("[%c]", str[i]);
}
return 0;
}
In Scala, you can use the split method to split strings at specific separators such as commas. split converts a string into an array of String elements and returns that array. For instance, if we have a string "a,b,c" and apply the split method on it, we would get an array of type String containing three elements: "a","b", and "c".
An array is a collection of elements of the same data type. For now, you can think of it as a variable that can store multiple values and each value has its own unique place. We will look at arrays in a later lesson in the course.
Let’s look at the syntax:
split takes one argument which lets the compiler know which separator it should split the string at. In our example, we are splitting at a comma, but you can choose any separator you want; "#", "*", etc.
xxxxxxxxxx
val splitPizza = "Pizza Dough,Tomato Sauce,Cheese,Toppings of Choice".split(",")
// Driver Code
splitPizza.foreach(println)