Solved

Need to reverse a string in Java

Posted on 2008-10-20
9
598 Views
Last Modified: 2012-05-05
Hi Folks,

I need an easy way to reverse a string in java, but not character by character, but separated by a fullstop.

For example .. I need

127.0.0.2
to be reversed to
2.0.0.127

Another example

192.168.0.1
to be reversed to
1.0.168.192

Any help would be much appreciated.

Cheers.
0
Comment
Question by:JigsawTech
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Easiest:  split into numbers,
reverse the numbers,
rebuild the string.

;JOOP!
0
 
LVL 20

Expert Comment

by:chaitu chaitu
Comment Utility
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 500 total points
Comment Utility
String original = "127.0.0.2";

String[] k = original.split("\\.");

String new = k[3] + '.' + k[2] + '.' + k[1] + '.' + k[0];

;JOOP!
0
 
LVL 1

Expert Comment

by:grahamcorp
Comment Utility
I would imagine you could break this into smaller substrings based on the '.'?
So for example,
Sorry if the syntax etc isn't right, but hopefully you get the idea?
May not be most efficient solution.

String ip = '192.168.0.1';
 

String [] tempIP = ip.split('.');
 

public static string reverseJoin( String[] ip, String delimiter)

{

   ip = reverseArray(ip);
 

   StringBuffer buffer = new StringBuffer();

   Iterator iter = ip.iterator();
 

   while(iter.hasNext())

   {

      buffer.append(iter.next());

      if( iter.hasNext() )

      {

          buffer.append(delimiter)

      }

   }

   return buffer.toString();

}
 

private static reverseArray(String [] arrayToReverse)

{

   String [] tempArray;
 

   for( int i = 0; i < tempIP.length; i++ )

   {

      tempArray[i] = tempIP[tempIP.length-i];

   }

   

   return tempArray;

}

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Expert Comment

by:grahamcorp
Comment Utility
I wasn't sure if the ip thing was just an example, i.e. is the string also going to be in the format <num>.<num>.<num>.<num>?
If it is then sciuriware is a better solution than mine as mine will be overkill.
0
 
LVL 9

Expert Comment

by:darkapple
Comment Utility
Ok here is a function i've created to reverse string as per your requirement.
	private String strRev(String str){

		

		String rev = "";
 

		String[] k = str.split("\\.");

		
 

		for(int i=k.length; i!=0; i--){

			

			rev = rev.concat(k[i-1]);

			rev = ((i-1)>0?rev.concat("."):rev.concat(""));

		}
 

		return rev;

	}

Open in new window

0
 
LVL 1

Author Closing Comment

by:JigsawTech
Comment Utility
Thanks alot, works great.
0
 
LVL 1

Author Comment

by:JigsawTech
Comment Utility
hi folks,

Appreciate the speedy responses .. didn't even think about using split..  doh.

Thanks again.
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Have a good time with avaJ.

!POOJ;
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now