-5
if(str.contains(keyword))
        {
            String cline = str.split(keyword)[1];
            String [] allwords = cline.split(" ");
            String data1 =  allwords[1];
            String data2 = allwords[2];
            String data3 = allwords[3];
            String data4 = allwords[4];
            System.out.println(rollno+" "+data1+" "+data2+" "+data3+" "+data4);
        }

I just want to know about this two Lines

line no 3 : what is happening there line no 4 : and what is happening there

Sorry for my bad English.

5 Answers5

1

Here is javadoc for split.

String cline = str.split(keyword)[1];

This line splits the String by supplied keyword, and extracts second token from it. E.g. if String is "This is an example" and keyword is "is" then value of cline would be "an example".

String [] allwords = cline.split(" ");

This line splits the string by space and stores the resulting tokens into allwords String array. E.g. if String is "This is an example" then value of allwords would be ["This", "is", "an" , "example"].

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • 1
    You should put that javadoc link **first** and explain to him that the **first** thing to do is always always look out for javadoc; instead of asking other people to explain stuff that is documented over and over already. – GhostCat Jan 10 '17 at 08:57
  • @GhostCat thanks for the input, I have updated the answer. – Darshan Mehta Jan 10 '17 at 09:07
  • Then you deserve an upvote; although answering such low quality questions is always ... well, tricky business. You see, as soon as this one gets closed (and it will be most likely be closed), I will put a delete request for it. So chances are, that this question ... and any answer will vanish at some point. – GhostCat Jan 10 '17 at 09:13
  • Fine, I will refrain from answering such questions then :) – Darshan Mehta Jan 10 '17 at 09:19
  • All I am saying is: dont surprised when such questions go away quickly. On the other hand, they attract a lot of answers, so to have any "chance" for upvotes, you need to deliver great quality. Which means to invest a lot of your time. So, it is about balancing cost and revenue ,-) – GhostCat Jan 10 '17 at 09:28
0

split will split the string by a token and return an array. The [] operator returns the value of the array in the specific place. so:

"a,b".split(",")[0] --> a

read this: How to split a string in Java

Community
  • 1
  • 1
galusben
  • 5,948
  • 6
  • 33
  • 52
0

Split method splits your string by the given character and returns an string array where each item is one piece of your original string.

This is:

Original String: "My String"

Splitted by " " item0: My item1: String

Splitted by "t" item0: My S item1: ring

if you add [number] after it then you keep just the item in the number position

SCouto
  • 7,808
  • 5
  • 32
  • 49
0
  • the 3rd line you splited your string based on the keyword found and took the second, token it's exactly like doing it like this

    String[] p = str.split(keyword); String cline = p[1]

  • the 4th line you just repeated the process above on the "cline" String and this time you splitted using a space .

mehdi maick
  • 325
  • 3
  • 7
0

String cline = str.split(keyword)1;
Line3: str.split creates an array of String by separating str by character 'keyword' and store its second element (String) in cline String reference.

Line4: creates another array of String by separating cline by character ' '.

String[] allwords = cline.split(" ");

For more information on split see How to split a string in Java

Community
  • 1
  • 1