-1

So if you are adding in a string, you can just add them via += method(the one i know and using atm). but how can you delete a word in a string/string array?

example: i have a string

String="Monday,Tuesday,Wednesday"

how do you make it into

String="Monday,Wednesday"

any help please?

ken
  • 113
  • 7

5 Answers5

1

its easy

just use

          yourString = yourString.replaceAll("the text to replace", "");  //the second "" show empty string so the text will get replace by empty string

finally yourString will contain the text u desire Thats it :)

Toppers
  • 559
  • 3
  • 11
  • I'm fairly sure he's not looking for a way to replace a specific word, but rather the index of the word. But that's just me... – Scherling Oct 21 '14 at 06:56
1

You could use the replace method.

String sentence = "Monday,Tuesday,Wednesday";
String replaced = sentence.replace("Tuesday,", "");
code monkey
  • 2,094
  • 3
  • 23
  • 26
  • so the "Tuesday" in the .replace is the word that i want to replace/delete and the empty string is the "delete"? – ken Oct 21 '14 at 07:00
  • Yes as written in the documentation. The first parameter is the target (sequence to replace) and the second one is the replacement (in your case it's just an empty string ""). – code monkey Oct 21 '14 at 07:02
  • What if the string was just "Monday,Tuesday" then this would fail. – JT Turner Oct 21 '14 at 07:40
  • It fails because "Monday,Tuesday" does not contain "Tuesday,"! Before posting you should try to think and understand. There is no comma "," in the end of the sentence. – code monkey Oct 21 '14 at 07:45
0

You can use the "public String replace(char oldChar, char newChar)" method if you want to remove "Tuesday" and not the second element

https://stackoverflow.com/questions/16702357/how-to-replace-a-substring-of-a-string

Community
  • 1
  • 1
PoisonedYouth
  • 548
  • 3
  • 16
0

I think, i will use it for simplicity otherwise go to other suggested answer...

Use Arraylist for storing days:

ArrayList<String> days = new ArrayList<String>();
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");        

Use it for creating days string:

        public String getDays() {

            String daysString = "";

            for (int i = 0; i < days.size(); i++) {
                if (i != 0)
                    daysString += ", ";
                daysString += days.get(i);
            }

            return daysString;
        }

And whenever you want to remove use

days.remove(1); 

or

days.remove("Tuesday");

then again call getDays();

IInd Method if you want to use only string:

String list = "Monday,Tuesday,Wednesday";
System.out.println("New String : " + removeAtIndex(list, 1));

and

public String removeAtIndex(String string, int index) {
        int currentPointer = 0;
        int lastPointer = string.indexOf(",");
        while (index != 0) {
            currentPointer = string.indexOf(',', currentPointer) + 1;
            lastPointer = string.indexOf(',', lastPointer + 1);
            index--;
        }

        String subString = string.substring(currentPointer,
                lastPointer == -1 ? string.length() : lastPointer);

        return string.replace((currentPointer != 0 ? "," : "") + subString
                + (currentPointer == 0 ? "," : ""), "");
    }
Chirag Jain
  • 1,612
  • 13
  • 20
  • 1
    If you do this I might use StringBuilder class to make the final string instead of +=. http://developer.android.com/reference/java/lang/StringBuilder.html – JT Turner Oct 21 '14 at 07:28
0

Something like this using a regular expression:

String contents = "Monday,Tuesday,Wednesday";
contents = contents.replaceAll("[\\,]+Tuesday|^Tuesday[\\,]*", "");
JT Turner
  • 502
  • 2
  • 14