4

When I grab the IP Address from my open socket, as someone has sent me a stream, I noticed that the IP has a forward-slash.

I just plan to simply omit it. But first I want to validate the first character in the string is the forward-slash:

String dataSource = data.getAddress().toString();

if(dataSource.substring(0,1) == "/"){
    System.out.println("trailing forward slash, delete it ");  
    dataSource = dataSource.substring(1);
}

This IF statement isn't being detected.

Anyone see what I'm doing wrong?

coffeemonitor
  • 12,780
  • 34
  • 99
  • 149
  • 2
    Use `.equals` to compare the content. `==` always compare by reference. – nhahtdh Dec 19 '12 at 04:15
  • @nhahtdh That is true. However, he is obviously a beginner(no offense) and there is no reason to downrate him for this. I counter with an upvote. – FrostyFire Dec 19 '12 at 04:16
  • legitamate question, ameture yes, but not a silly question and was worded correctly. – Sean F Dec 19 '12 at 04:18
  • @Sean F yeah, he is trying. I have been in his shoes before with stuff like this. – FrostyFire Dec 19 '12 at 04:19
  • Take a look at comparing references vs primitive data types (Srinivas B has a good point). After you get that, take a look at string object pooling and interning just to confuse yourself a little bit! ;) – Marsellus Wallace Dec 19 '12 at 04:36
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Don Roby Dec 19 '12 at 11:45

11 Answers11

2

For string comparisons use equals method, as it is more reliable than using == operator (it compares the content while == comnpares the references ) :

Try using equals method :

if(dataSource.substring(0,1).equals("/")){
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
2

please use .equals

if("/".equals(dataSource.substring(0,1))){
    System.out.println("trailing forward slash, delete it ");  
    dataSource = dataSource.substring(1);
}

instead of ==

Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
2

If you want to test only, the first character , you can try the method,

dataSource.charAt(0)=='/'

Srinivas B
  • 1,821
  • 4
  • 17
  • 35
1

you should use .equals for string comparrisons so

if(dataSource.substring(0,1) == "/")

should be

if ("/".equals(dataSource.substring(0,1)))
Sean F
  • 2,352
  • 3
  • 28
  • 40
0

You're comparing strings with ==, which means you're checking to see if these two strings are literally the same string reference.

Use equal() to check and see if two strings contain the same characters.

See: http://www.leepoint.net/notes-java/data/expressions/22compareobjects.html

Michael
  • 3,334
  • 20
  • 27
0

Use .equals(Object) or .equalsIgnoreCase(String) for comparing Strings.

Timr
  • 1,012
  • 1
  • 14
  • 29
0

You need to do dataSource.substring(0,1).equals("/"), which actually checks the content of the strings. == only checks whether both sides are the same object.

darcyy
  • 5,236
  • 5
  • 28
  • 41
0

Try this

if (("some string to check").equals(myString)){ doSomething(); }

Asish AP
  • 4,421
  • 2
  • 28
  • 50
0

Always use equals when comparing values. Having said that you can just use indexOf method for your purpose

   if (dataSource.indexOf('/') == 0) {
        dataSource = dataSource.substring(1);
    }
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
0
String dataSource = "/hell";

        if(dataSource.substring(0,1).equalsIgnoreCase("/")){
            System.out.println("trailing forward slash, delete it ");  
            dataSource = dataSource.substring(1);
        }

run this program. this is working..

QuokMoon
  • 4,387
  • 4
  • 26
  • 50
0

Why use a String? You're just creating this problem for yourself. Use the datatype provided:

InetAddress dataSource = data.getInetAddress();

or

SocketAddress dataSource = data.getRemoteSocketAddress();

Then you have a semantically-aware .equals() method in both cases.

user207421
  • 305,947
  • 44
  • 307
  • 483