2

I have words like:

Sams – like costco
Jecy penny ? like sears

In Java I want to take this string and get the out put as:

Sams 
Jecy penny 

Is there any way I can remove all characters after - and ??

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
The Learner
  • 3,867
  • 14
  • 40
  • 50
  • One of the way is using String class [split method](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29) another is using [substring](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#substring%28int,%20int%29). – kosa Aug 17 '12 at 06:02

4 Answers4

6

Three options:

  • Use indexOf and then substring
  • Use split and then take the first return value
  • Use regular expressions to replace everything after the first part

Here's the split option - bear in mind that split takes a regular expression:

public class Test {

    public static void main(String[] args) {
        showFirstPart("Sams - like costco");
        showFirstPart("Jecy penny ? like sears");
    }

    private static void showFirstPart(String text) {
        String[] parts = text.split("[-?]", 2);
        System.out.println("First part of " + text
                           + ": " + parts[0]);
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Jon did not work...for the following: String text1 = " Sams Best – costco is ok "; the answer is same. I was not able to remove after - – The Learner Aug 17 '12 at 06:33
2

1. Sams - like costco

Answer:

String s = "Sams - like costco";

String[] arr = s.split("-");

String res = arr[0];

2. Jecy penny ? like sears

Answer:

String s = "Jecy penny ? like sears";

String[] arr = s.split("\\?");  

Added \\ before ?, as ? has a special meaning

String res = arr[0];

Though the above 2 examples are for those with only one "-" and "?", you can do this for multiple "-" and "?" too

THE OUTPUT

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • I tried String[] arr = text.split("-"); String res = arr[0]; and the text is = Sams Best – costco is ok . It did not work – The Learner Aug 17 '12 at 06:40
  • yes did not work String text1 = " Sams Best – costco is ok "; String[] arr = text1.split("-"); String res = arr[0]; System.out.println(res);===output is  Sams Best – costco is ok – The Learner Aug 17 '12 at 06:57
  • Sorry for the inconvenience, i forgot to add the "\\" character before ?, see my edited answer... – Kumar Vivek Mitra Aug 17 '12 at 06:58
0

Use String.split() method

String str = "Sams – like costco";
    String str1 = "Jecy penny ? like sears";

    String[] arr = str.split("–");
    String arr1[] = str1.split("\\?");
    System.out.println(arr[0]);
    System.out.println(arr1[0]);
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
0

I've assumed that you want to remove the word "like" and the following word after either "-" or "?"...

Here's how you do it in one line:

String output = input.replaceAll( "(?i)[-?]\\s*like\\s+\\b.+?\\b", "" );

Here's some test code:

public static void main( String[] args ) {
    String input = "Sams - like costco Jecy penny ? like sears";
    String output = input.replaceAll( "(?i)[-?]\\s*like\\s+\\b.+?\\b", "" );
    System.out.println( output );
}

Output:

Sams  Jecy penny 
Bohemian
  • 412,405
  • 93
  • 575
  • 722