Nikolay
Nikolay

Reputation: 341

How to split a sentence into two parts

How to split a sentence into two parts in JAVA? If there is the following

String sentence = "I love Java <=> I love Python"

How can I return I love Java and I love Python thus separately ignoring <=>?

public void changeSentence(String line)
{
    String[] words = line.split(" ");

    for(int i = 0; i < words.length; i++)
    {
        if(!(words[i].equals("<=>")))
        {


        }
    }
}

Upvotes: 2

Views: 2395

Answers (5)

Palak
Palak

Reputation: 1296

It can be done using the method given below of class String

METHOD: (public String[] split(String regex, int limit)
  • Regex: The String/Character you wish to remove & split remaining text
  • Limit: How many String that should be returned
public class TestSplit

{

    public static void main(String args[])
    {
        String str = new String("I Love Java <=> I Love Python");
        
    
        for (String retval: str.split("<=> ",2))
        {
            
                System.out.println(retval);
        }


    }
}

Output:

I Love Java

I Love Python


There are some other facts I am aware about are listed below

  • If you won't specify limit by 'keeping it blank'/'specify 0' then the compiler will split string every time '<=>' is found e.g.
public class TestSplit

{

    public static void main(String args[])

    {

        String str = new String("I Love Java <=> I Love Python <=> I Love Stackoverflow");
        for (String retval: str.split("<=> "))
        {
                System.out.println(retval);
        }


    }
}

Output:

I Love Java

I Love Python

I Love Stackoverflow

Upvotes: 4

Paramvir Singh Karwal
Paramvir Singh Karwal

Reputation: 616

How can I return I love Java and I love Python thus separately ignoring <=>?

First of all as you have said that you want your method to return separate words (Strings technically), for that you need change your return type from void to String[ ]

Second, you are using String[] words = line.split(" "); this will split the String where spaces appear which would yield you array of Strings containing

I
love
Java
<=>
I 
love
Python

as different words stored as separate Strings in your words array.

so what you should do is Strings[] words=line.split("<=>"); and return words

Full code should be like this

public String[] changeSentence(String line)
{
    String[] words = line.split("<=>");
    return words;
}

Upvotes: 0

VSK
VSK

Reputation: 250

You can also split with StringTokenizer.

The code for splitting the strings based upon delimiter is below:

StringTokenizer stringTokenizer = new StringTokenizer(sentence,"<=>");
        while(stringTokenizer.hasMoreTokens()) {
            System.out.println(stringTokenizer.nextToken());
        }

I hope this helps

Thanks

Upvotes: 0

Bhashkar
Bhashkar

Reputation: 11

public String[] changeSentence(String line){
String[] substrings = line.split("<=>");
return substrings;
}

Upvotes: 0

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

Why not do:

String[] words = line.split("<=>");
for(String word : words){
        System.out.println(word); 
}

Output:

I love Java

I love Python

Upvotes: 0

Related Questions