1

Ok so I'm trying to create a method that takes 2 strings, the second string argument is the string to be removed from the first string argument. I can't figure out what's wrong with my code? Any help? Thanks.

Example of what I'm trying to do: if you pass in "a,b,c" as the first argument and "," as the second argument, you get "abc" returned.

public List<String> split(String string, String delimiter){
    //create and init arraylist.
    List<String> list = new ArrayList<String>();
    //create and init newString.
    String newString="";
    //add string to arraylist 'list'.
    list.add(string);
    //loops through string.
    for(int i=0;i<string.length();i++){
        //stores each character from string in newString.
        newString += string.charAt(i);

        }
    newString.replace(delimiter, "");
    //remove string from arraylist 'list'.
    list.remove(string);
    //add newString to arraylist 'list'.
        list.add(newString);


    return list;
5
  • Can you please give an example of what you are trying to do? Commented May 9, 2013 at 2:00
  • 4
    Please add a tag to indicate what language this is. Commented May 9, 2013 at 2:01
  • Have you looked into String.split (docs.oracle.com/javase/1.5.0/docs/api/java/lang/…)? Commented May 9, 2013 at 2:06
  • 2
    If a,b,c is a string and you want string abc, why cann't you just do yourString.replaceAll(secondArg, ""); Commented May 9, 2013 at 2:09
  • why is the return type a List, shouldn't it be a String Commented May 9, 2013 at 2:45

3 Answers 3

1

Pretty simple, not sure what you're doing with those lists, but based on your worded question, if all you want is abc from a,b,c, this will do it...

 public String split(String s1, String s2){
        return s1.replace(s2,"");
 }
Sign up to request clarification or add additional context in comments.

2 Comments

this is exactly what I have in comment.....I did not post an answer because this returns string and the question has List
I felt the OP's use of lists was overkill and unnecessary when replace() achieves the same result with far less code. Don't be afraid to improve the OP's solution if you feel it can help, cheers.
1

Judging by method name, return type and parameter names, it seems like you're attempting to reinvent the wheel - you only need one line:

public List<String> split(String string, String delimiter){
    return Arrays.asList(string.split("\\" + delimiter));
}

I put the backslash in there in case the delimiter itself is a regex, like a dot.

1 Comment

Those loops took me for a loop, glad I wasn't the only one :-)
0
public List<String> split(String str, String delimiter){
    //Is the List of strings mandatory? Or is it part of a solution
    //that didn't originally have to be so?

    String[] temp;
    List<String> newStrings=new ArrayList<String>(); 

    //The answer was split(String regex) all along!
    temp = str.split(delimiter);

    for(int i=0; i<temp.length; i++){
        newStrings.add(temp[i]);
    }

    return newStrings;
}

}

This will produce the desired specifications you have, but be warned that this will include spaces as individual strings. I will update this if you request it exclude spaces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.