• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

remove "." from string

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting an IPaddress in this form:

String ip_address = "172.17.91";

I want to remove the "." and convert this to an integer e.g. (Convert "172.17.91" to 1721791)
But replaceAll() is not working and if I use replace() then I am unable to substitute "." with blank spaces. And if I substitute with a blank space then parseInt() doesn't works.

Thanks,
Nee
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nee Kat:
I am getting an IPaddress in this form:

String ip_address = "172.17.91";

I want to remove the "." and convert this to an integer e.g. (Convert "172.17.91" to 1721791)

But replaceAll() is not working and if I use replace() then I am unable to substitute "." with blank spaces. And if I substitute with a blank space then parseInt() doesn't works.



first of all, "172.17.91" is not an IP address - you need four quads in an IP address, and that string only has three.

second, i'm not sure that what you seem to want to do makes any sense. an IP address is, at the lowest level, a positive integer number - that much is true - but if that's the format you want it in, then the method you've chosen won't work for getting it there. you'll need to convert each quad to a byte (well, octet), individually, then left-shift each as appropriate and add them.

thirdly, exactly how does replaceAll() fail? maybe we can fix that for you, if you'll tell us what it's doing wrong.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heres some code that works dor your question:


String ip_address = "172.17.91";
int j = 0, h=0;
char period = '.';
char array [] = new char[ip_address.length()];

for(int x = 0; x < ip_address.length(); x++){

if(!(ip_address.charAt(x) == period)){

array[j] = ip_address.charAt(x);
j++;}
else{h++;}



}

for(int i =0; i < ip_address.length()-h; i++){

System.out.print(array[i]);
}

[ March 08, 2005: Message edited by: Kevin Peterson ]
[ March 08, 2005: Message edited by: Kevin Peterson ]
 
Kevin Peterson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do Keep in mind that the code i posted does work but it isnt efficient at all, that was just off the top of my head so please critique it as needed.

-Kp
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way I would do it is to use a String Tokenizer with the "." as the delimiter, then add each token to a new string (in this case, only 3).
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would do it this way (also because its the only way i know how)

int len;
string ip_address;
int i;

len = ip_address.length();

for (i=0; i<=str.length(); i++){
if (ip_address.charAt(i) == "."){
System.out.print(" ");
}
}

or something like that...i think you get the idea, its just a for loop
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nee Kat:
I want to remove the "." and convert this to an integer e.g. (Convert "172.17.91" to 1721791)
But replaceAll() is not working and if I use replace() then I am unable to substitute "." with blank spaces. And if I substitute with a blank space then parseInt() doesn't works.



Are you doing replaceAll(".","")? If you are, then that is the problem. A "." has special meaning in a regular expression, and will match anything. You will effectively replace everything with nothing.

Of course, since you are not telling us exactly what you are doing, this is purely a guess.

Henry
 
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use this to remove any non-digit in your String
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nee Kat:
I am getting an IPaddress in this form:

String ip_address = "172.17.91";

I want to remove the "." and convert this to an integer e.g. (Convert "172.17.91" to 1721791)
But replaceAll() is not working and if I use replace() then I am unable to substitute "." with blank spaces. And if I substitute with a blank space then parseInt() doesn't works.

Thanks,
Nee



I very much suspect you don't actually want to do this. Doing this makes the number ambiguous - not a good characteristic for an address!

E.g. 17217911 could be any of the following: 17.217.9.11, 172.179.1.1, 17.2.179.11 etc.

As M Beck points out, what you probably want to do is convert it from dotted decimal form, and pack the bytes into another type.

To achieve this, you might want to look at the
InetAddress class, and in particular the getAddress() method.

This return a byte array which you can then pack into an appropriate primitive type. I'll leave this bit up to you

Finally be aware that there are 32-bit IPv4 addresses which are 32-bits and IPv6 addresses which are 128-bit.
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Are you doing replaceAll(".","")? If you are, then that is the problem. A "." has special meaning in a regular expression, and will match anything. You will effectively replace everything with nothing.



Use replaceAll("\\.", "") instead of replaceAll(".","").
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you can see from the amount of different responses (all good, by the way), it's hard to really help you until you tell us more details...
reply
    Bookmark Topic Watch Topic
  • New Topic