-3

I was trying to split my string using ± (alt + 0177) sign, but it dos't detect it. I also tried indexOf() but its not work

    String myString = "20±1";
    if(myString.indexOf('±')>-1){
         System.out.println("We are in here.........");
    }
Amita Patil
  • 1,310
  • 2
  • 14
  • 22

4 Answers4

1

Use function split()

String myString = "20±1";
String result[] = myString.split("±");
//result[0] = 20
//result[1] = 1
1

You can use the ascii value for the '±' sign.

An easy way get the ascii value as shown in this reply here

In your case:

final int ascii = (int) '±';
final String myString = "20±1";

if(myString.indexOf(ascii)>-1){
     System.out.println("We are in here.........");
}
martidis
  • 2,897
  • 1
  • 11
  • 13
0
    /*Your String*/
    String myString = "20±1";


    /*If you want to split String you can use String.split("your string regex here")
     * and it will create String array without specified string regex with left, right 
     * side of string or multiple strings depending on occurrence of specified string regex*/

    String[] splitted = myString.split("±");


    /*Just to validate output*/
    System.out.println(Arrays.toString(splitted));  
Samuel.P
  • 38
  • 11
0

you can use also StringTokenizer for this problem:

import java.io.*;
import java.util.*;
class happy {
    public static void main(String args[])
    {
        String myString = "20±1";
        StringTokenizer st=new StringTokenizer(myString,"±");
        String a="";
        while(st.hasMoreTokens())
        {
            a=st.nextToken();
            System.out.println(a);
        }
    }
}
Rohit-Pandey
  • 2,039
  • 17
  • 24