2

I wonder how to split a number on parts and then compare them to each other

For instance, I've got a set of numbers

 989010
 990009
 991008
 992992
 993006
 994005
 995004

and I want to divide each of numbers on two parts -->>

if it's 989010 -- it will be like '989' and '010'. 

After that, I think I can just compare two strings, right?

I mean,

   '989' != '010'  true
   '990' != '009'  true 
   '992' != '992'  false

it seems I should use the split function, however I confused how to separate only on two parts rather than more

Thanks in advance!

Leo
  • 1,787
  • 5
  • 21
  • 43

5 Answers5

9
String str = "989010";

System.out.println(str.substring(0, 3).equals(str.substring(3,6)));
NPKR
  • 5,368
  • 4
  • 31
  • 48
4

You could do this is several ways.

Either String.substring which would give you two strings or you could divide the numbers

int number = 123456;
int firstPart = number / 1000;
int secondPart = number - firstPart * 1000;

EDIT
Sorry for that - was in a rush.
To make up for it I will once again prove that any problem worth solving can be solved with the (often baffling) language of regex:

    final String[] strings = new String[]{"123123", "123456"};
    final Pattern pattern = Pattern.compile("([\\d]{3})\\1");
    for (final String string : strings) {
        final Matcher matcher = pattern.matcher(string);
        if (matcher.matches()) {
            System.out.println(string + " matches.");
        }
    }

Output:

run:
123123 matches.
BUILD SUCCESSFUL (total time: 0 seconds)

And it even takes 0 seconds, ha.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
1
    Long number = 989010L;
    String text = number.toString();
    String firstPart = text.substring(0,3);
    String secondPart = text.substring(3);

    Long firstNumber = Long.parseLong(firstPart);
    Long secondNumber = Long.parseLong(secondPart);

    System.out.println(firstNumber == secondNumber);
Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33
1

LOOK AT THIS

public static void main(String[] args) {

        String[] values = { "989010", "990009", "991008", "992992", "993006", "994005", "995004" };

        for(String value : values) {
            String firstPart = value.substring(0, 3);
            String secondPart = value.substring(3);
            if(firstPart.equals(secondPart)) {
                System.out.println(firstPart + " equals " + secondPart);
            }
        }
    }
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
1
`String s = "931492";
 int first = Integer.parseInt(s.substring(0, s.length()/2));
 int second = Integer.parseInt(s.substring(s.length()/2));
 if (first != second)
 {
    System.out.println("Not equal");
 }`

EDIT: Right i was comparing the wrong Substrings, sorry for that. Also you should use equals() when comparing Strings in Java. I updated my code to generate two Integer. Might provide additional comparing capabilities.

Christian Rapp
  • 1,853
  • 24
  • 37
  • 2
    `==` and `!=` used on `String` - Compares references and not the content itself. They'll always be `Not equal` unless they point to the same `String` in the `String pool`. – Maroun Feb 19 '13 at 09:00