4 Replies - 1197 Views - Last Post: 02 October 2009 - 05:35 PM Rate Topic: -----

#1 tony.vanhorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-September 09

String Manipulation

Posted 02 October 2009 - 09:25 AM

the application I'm writing receives a udp packet and converts to a string. I need to get rid of the unwanted excess characters and unscramble the needed content. Basically I'm looking at the characters 12 thru 31 (in the string received) as the needed content but need them ordered as follows: [13][12][15][14][17][16]... and so on.

I think that I want to do character array or string buffer manipulation. What do the geniuses of this forum suggest, and can you suggest a for loop that will work for unscrambling the numbers they way that I need.

	public String convertPackettoString (byte rxbuffer[], int dataLen )
	{

		stringpacket = new String(rxbuffer,0,dataLen);		
		System.out.println(stringpacket); //generates the following string "+CRSM:144,0,98101430122149074180"
		//Desired string result:  "89014103211294701408"

		return atResults;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: String Manipulation

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: String Manipulation

Posted 02 October 2009 - 10:13 AM

If you know that this will always be the order, you can do something like this:

byte[] bufferDigits; //assuming it has the characters in it
int[] indexes = {13, 12, 15, 14...};
String x = "";
for(int i:indexes){
   x += bufferDigits[i]; 
}


Was This Post Helpful? 0
  • +
  • -

#3 tony.vanhorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-September 09

Re: String Manipulation

Posted 02 October 2009 - 10:20 AM

View Postmacosxnerd101, on 2 Oct, 2009 - 09:13 AM, said:

If you know that this will always be the order, you can do something like this:

byte[] bufferDigits; //assuming it has the characters in it
int[] indexes = {13, 12, 15, 14...};
String x = "";
for(int i:indexes){
   x += bufferDigits[i]; 
}




That's the cleanest solution I've seen yet. I'll give it a try.
Was This Post Helpful? 0
  • +
  • -

#4 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: String Manipulation

Posted 02 October 2009 - 05:24 PM

The only issue with mac's solution is if the string has an unknown length. If its a constant length then the solution is fine. Another approach is simply to rebuild the string in the correct order

public class StringReorder {

	StringBuilder reordered = new StringBuilder();

	public StringReorder(String input) {
		for (int i = 2; i <= input.length(); i += 2) {
			reordered.append(input.charAt(i - 2)).append(input.charAt(i));
		}
	}

	public String getReorderedString() {
		return reordered.toString();
	}
}


This post has been edited by skaoth: 02 October 2009 - 05:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: String Manipulation

Posted 02 October 2009 - 05:35 PM

I know what you mean, Skaoth; however, I saw that the OP said:

Quote

Basically I'm looking at the characters 12 thru 31...
but need them ordered as follows: [13][12][15][14][17][16]... and so on.


If I had a formula to work on rather than given indices, it would be easier to develop a more adaptable solution. However, if the OP doesn't have a formula and needs them in this order, then my solution will work fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1