Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Discover how organizations are unlocking new revenue streams: Watch here
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Manipulate strings, replace a blank space on two

Hi all,

 

I try to transform an string into another one.


My source string have blank spaces, and i want to replace some of them by a comma. More precisely, i want to change just one on two.


An example could be more efficient:

 

source string ( a list of coordinates) :
45.979904 -1.355741 45.979665 -1.355603 45.979396 -1.35542 45.979095 -1.355198 45.978451 -1.354722

 

Desire result:
45.979904 -1.355741,45.979665 -1.355603,45.979396 -1.35542,45.979095 -1.355198,45.978451 -1.354722

 

As you can see, the first blank space is not replaced, but the second yes, the third one no and fourth yes...

 

Unfortunately, i didn't know how many couples of coordinates i will have.

 

Somebody have an idea?

 

Thank you.

Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

Ah, sorry. I assumed you might know some Java. OK do the following....

 

1) Create a routine called MyRoutine (you can change the name if you choose later, once you understand this).

2) Replace the code that is automatically generated with the below...

package routines;


public class MyRoutine {

	public static String prepareCoords(String data){
	      String[] tokens = data.split(" ");
	      String returnVal = "";

	      for(int i = 0; i<tokens.length;i++){
		   if(i%2==1){
			returnVal = returnVal+tokens[i]+",";
		   }else{
			returnVal = returnVal+tokens[i]+" ";
		   }	
	     }
	     return returnVal;
	}
}

3) Save the routine.

4) Now, in your job you would use the routine like below. I will assume that you are using it in a tMap or similar and that your data is in a column called "MyData" and the row is "row1".....

routines.MyRoutine.prepareCoords(row1.MyData)

The above code would do in one of your output columns in a tMap

View solution in original post

16 Replies
Anonymous
Not applicable
Author

Create a routine to do this. I've knocked the below up which *should* work. You will need to test it first...

public static String prepareCoords(String data){
      String[] tokens = data.split(" ");
      String returnVal = "";

      for(int i = 0; i<tokens.length;i++){
	   if(i%2==1){
		returnVal = returnVal+tokens[i]+",";
	   }else{
		returnVal = returnVal+tokens[i]+" ";
	   }	
     }
     return returnVal;
}
Anonymous
Not applicable
Author

Thank you rhall_2_0 

 

There is several error message displayed in the interface:

 

public static String prepareCoords(String data){

- syntax error on token(s), misplaced construct(s)

- syntax error on token(s), misplaced construct(s)

- Syntax error on token "String", @ expected

 

String[] tokens = data.split(" ");

- syntax error on token(s), misplaced construct(s)

 

String returnVal = "";

- syntax error on token(s), misplaced construct(s)

 

And the last one:

0683p000009Lw5t.png

I am not familiar at all with java and the routines, but I have found how to use it so I will be able to test it I think.

Anonymous
Not applicable
Author

Ah, sorry. I assumed you might know some Java. OK do the following....

 

1) Create a routine called MyRoutine (you can change the name if you choose later, once you understand this).

2) Replace the code that is automatically generated with the below...

package routines;


public class MyRoutine {

	public static String prepareCoords(String data){
	      String[] tokens = data.split(" ");
	      String returnVal = "";

	      for(int i = 0; i<tokens.length;i++){
		   if(i%2==1){
			returnVal = returnVal+tokens[i]+",";
		   }else{
			returnVal = returnVal+tokens[i]+" ";
		   }	
	     }
	     return returnVal;
	}
}

3) Save the routine.

4) Now, in your job you would use the routine like below. I will assume that you are using it in a tMap or similar and that your data is in a column called "MyData" and the row is "row1".....

routines.MyRoutine.prepareCoords(row1.MyData)

The above code would do in one of your output columns in a tMap

Anonymous
Not applicable
Author

Perfect thank you very much.

 

I combine this with a StringHandling.LEFT and a concatenation to add at the end of the result the first couple of coordinats (that is a prerequisite of WKT).

I will just have to tansform my string into a geometry to set a projection and the work will be done.

 

Thanks again for your help.

Anonymous
Not applicable
Author

Hi,

 

You could use a tReplace component:

0683p000009Lw3x.png

Eric

Anonymous
Not applicable
Author

Hello,

 

I may need some help again, a detail have been forgotten...

 

In the string submit at the beginning, I didn(t see that the X and Y are reversed. In fact when i have this as a source string ( a list of coordinates) :
45.979904 -1.355741 45.979665 -1.355603 45.979396 -1.35542 45.979095 -1.355198 45.978451 -1.354722

 

The result might be:
-1.355741 45.979904,-1.355603 45.979665,-1.35542 45.979396,-1.355198 45.979095,-1.354722 45.978451

 

Is there a way to reverse order of data inside each couple of coordinates?

Anonymous
Not applicable
Author

Hi,

 

With a tReplace:

0683p000009LvwO.png

 

-First expression (To add a space at the end of your line):

pattern is "(.*)"

replace is "$1 "

-Second expression (To reverse the coordinates and replace the space with a comma)

pattern is "(.*?)( )(.*?)( )"

replace is "$3 $1,"

-Last expression (To remove the comma at the end of the line):

pattern is "(.*),"

replace is "$1"

 

Let me know it it meets your requirements

 

Eric

Anonymous
Not applicable
Author

For first expression pattern should be "(^.*)" (Otherwise you will get an extra space at the end of the line)

Anonymous
Not applicable
Author

Hello Eric,

 

I have duplicate the job and test this solution.

Here is the result:

-0.447106,44.994297 44.994234,-0.446109,44.994333 -0.446381,44.994321,-0.445707,44.994443 -0.446009,44.99437,-0.444517,44.994516 -0.445193,44.9945,-0.443701,44.994416 -0.444318,44.99447,-0.443013,44.994124 -0.44327,44.994384,-0.44093,44.993959 -0.441486,44.994033,-0.440272, -0.440493,44.993887

 

The original data was: 

44.994234 -0.447106,44.994297 -0.446381,44.994321 -0.446109,44.994333 -0.446009,44.99437 -0.445707,44.994443 -0.445193,44.9945 -0.444517,44.994516 -0.444318,44.99447 -0.443701,44.994416 -0.44327,44.994384 -0.443013,44.994124 -0.441486,44.994033 -0.44093,44.993959 -0.440493,44.993887 -0.440272 ( Bad copy/paste)

 

44.994234 -0.447106 44.994297 -0.446381 44.994321 -0.446109 44.994333 -0.446009 44.99437 -0.445707 44.994443 -0.445193 44.9945 -0.444517 44.994516 -0.444318 44.99447 -0.443701 44.994416 -0.44327 44.994384 -0.443013 44.994124 -0.441486 44.994033 -0.44093 44.993959 -0.440493 44.993887 -0.440272

 

For more efficience, I suggest to simplify the original data into:

Y1 X1 Y2 X2 Y3 X3...

 

As you can see, the result is :

X1(comma)Y2(space)Y1(comma)X3(comma)Y3...

or

X1,Y2 Y1,X3,Y3...

and the expected is:

X1 Y1,X2 Y2, X3 Y3...

 

If i am not cear enought tell me of course.

 

Thank you for helping me anyway