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?

6
  • 2
    Use .equals to compare the content. == always compare by reference. Commented Dec 19, 2012 at 4: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. Commented Dec 19, 2012 at 4:16
  • legitamate question, ameture yes, but not a silly question and was worded correctly. Commented Dec 19, 2012 at 4:18
  • @Sean F yeah, he is trying. I have been in his shoes before with stuff like this. Commented Dec 19, 2012 at 4: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! ;) Commented Dec 19, 2012 at 4:36

11 Answers 11

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("/")){
Sign up to request clarification or add additional context in comments.

Comments

2

please use .equals

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

instead of ==

Comments

2

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

dataSource.charAt(0)=='/'

Comments

1

you should use .equals for string comparrisons so

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

should be

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

Comments

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

Comments

0

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

Comments

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.

Comments

0

Try this

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

Comments

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);
    }

Comments

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..

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.