Skip to main content

APIzation: Replication Package

[Q#8646441][A#8646467] how can I get the text before and after the "-" (dash)

https://stackoverflow.com/q/8646441

I have a String and I want to get the words before and after the " - " (dash). How can I do that? example: String: output:

Answer

https://stackoverflow.com/a/8646467

With no error-checking or safety, this could work:

APIzation

String[] parts = theString.split("-");
String first = parts[0];
String second = parts[1];
package com.stackoverflow.api;

public class Human8646467 {

  public static String[] splitByDash(String theString) {
    String[] parts = theString.split("-");

    return parts;
  }
}

package com.stackoverflow.api;

/**
 * how can I get the text before and after the "-" (dash)
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/8646467">https://stackoverflow.com/a/8646467</a>
 */
public class APIzator8646467 {

  public static String getText(String theString) throws Exception {
    String[] parts = theString.split("-");
    String first = parts[0];
    return parts[1];
  }
}