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;
}
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
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);
}
}
}
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)