Тёмный

How to split a given String into substrings while specifying a delimiter using Java split() method 

Career & Tech HQ
Подписаться 11 тыс.
Просмотров 20 тыс.
50% 1

How to split a given String into substrings while specifying a delimiter using the Java split() method
In this video, we will discuss about the Java split() method, which can be used to split a given String into substrings while specifying a delimiter.
One important point to note here is that, this method returns an array of strings and the split() method has two variations
1. In the first example, I will show you how to split a given String into substrings and have it return an array of Strings
String str1 = "23/05/2020";
Since we are dealing with arrays, I am going to declare an array that will store the returned values whenever the method is executed
String substrings[] = str1.split("/");
"/" here is the delimiter, this means that wherever the method finds this character (/) in the String, it will split the given String at that precise position
Now we need to find a way to output all the values stored in the array
Doing a System.out.println(substrings); will not work
We will rather use the Arrays.toString() method, by writing
System.out.println(Arrays.toString(substrings));
We could also output each substring at every array index position
System.out.println(substrings[0]);
System.out.println(substrings[1]);
System.out.println(substrings[2]);
...
2. In the second example, I will show you how to limit the number of substrings to be returned after splitting a given String
String str1 = "23/05/2020";
Since we are dealing with arrays, I am going to declare an array that will store the returned values whenever the method is executed
String split[] = str1.split("/" , 2);
"/" here is the delimiter, this means that wherever the method finds this / character, it will split the given String at that precise position
And 2 represents the limited number of substrings to output. This method, when evaluated, is supposed to return an array of only two Strings even when the delimiter is present in the array more than two times.
Now we need to find a way to output all the values stored in the array
Doing a System.out.println(substrings); will not work
We will rather use the Arrays.toString() method, by writing
System.out.println(Arrays.toString(split));
Note that our array contains only two substrings, exactly as we indicated it in the method statement.
Also, the delimiter is present in our array more than two times.
#codingriver
#java
#programming

Опубликовано:

 

2 июн 2020

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 9   
@someshsahu4638
@someshsahu4638 2 года назад
Very nice explanation sir thankyou so much 🙏🏼 Very helpful video 🙏🏼🙏🏼🙏🏼
@zainabalayande278
@zainabalayande278 Год назад
Thank you. This as an eye opener to me, just learning about it for the first time. Please I would like to ask, how can I get the sum of a substring? Like adding 23 together to get 5 (a single number).
@mudaseerahmed3944
@mudaseerahmed3944 2 года назад
Nice
@zeroin7819
@zeroin7819 3 года назад
Thank you
@careerandtechhq
@careerandtechhq 3 года назад
Thanks you liked it
@oScorpz
@oScorpz 2 года назад
thanks brother
@uppalaadarsh3261
@uppalaadarsh3261 3 года назад
I want split a CSV file which contains only one record and every word is comma separated but I want split after every 4 occuranses of comma , is it possible ??
@azeemmirza9855
@azeemmirza9855 2 года назад
Imagine you learned how to use split method and the interviewer ask you to split the string wothout split method.🚪🏃💨
@glam-jh9wz4mk7c
@glam-jh9wz4mk7c Год назад
it's buler