Lr Ayman
Lr Ayman

Reputation: 17

a simple way to break a String into a list of Strings

so i am still learning java and recently i needed a method that does this :

it breaks the string wherever it finds a certain charecter, lets say a 'space' is where it breaks

String s= "aaa bbb eejj ,hhre";
String[] list = breaktheString(s);
for(String g : list){
   System.out.println(g);
}

it shows this:

aaa
bbb
eejj
,hhre

thats it but i need to write this to meet the quality standards but thats actually all i have so just ignore this and act like i didnt write it down

Upvotes: 0

Views: 67

Answers (2)

flyingCode
flyingCode

Reputation: 152

Since you're learning I don't want to give you the complete answer in case this is homework, but i'll give you a couple of hints. You can check if a certain character is in a certain part of a string with an if like this:

//checks if space is in the first position of str
class Main {
    public static void main(String[] args) {
        String str = " hello";
        if (' ' == str.charAt(0) {
            System.out.println("the first character is a space);
        }
    }
}

Upvotes: 0

akuzminykh
akuzminykh

Reputation: 4723

String s= "aaa bbb eejj ,hhre";
String[] list = s.split(" ");
for(String g : list){
   System.out.println(g);
}

Upvotes: 2

Related Questions