Talend String handling convert "0.12900-" string to -0.12900 in float - talend

Need help with the below conversion in Talend:
"0.12900-" string to -0.12900 in float via Tmap expression.
I am not well versed with Java hence the difficulty.

You could try something like this :
row1.column.contains("-")?Float.parseFloat( "-"+ StringHandling.LEFT(row1.column,row1.column.length()-1)):Float.parseFloat(row1.column)
Float.parseFloat allows you to convert a string to a float type.
StringHandling.LEFT gets the first characters of a string, here the total length-1.
Ternary operator controls if your string contains "-", otherwise you just have to parse the "-" symbol

Related

What is the most effective way in systemVerilog to know how many words a string has?

I have Strings in the following structure:
cmd, addr, data, data, data, data, ……., \n
For example:
"write,A0001000,00000000, \n"
I have to know how many words the String has.
I know that I can go over the String and search for the number of commas, but is there more effective way to do it?
UVM provides a facility to do regexp matching using the DPI, in case you're already using that. Have a look at the functions in uvm_svcmd_dpi.svh
Verilab also provides svlib, a package containing string matching functions.
A simpler option would be to change the commas(,) to a space, then you can use $sscanf (or $fscanf to skip the intermediate string and read directly from a file), assuming each command has a maximum number of words.
int code; // returns the number of words read
string str,word[5];
code = $sscanf(str,"%s %s %s %s %s", word[0],word[1],word[2],word[3],word[4]);
You can use %h if you know a word is in hex and translate it directly to a numeric value instead of a string.
The first step is to define extremely clearly what a word actually is vis. what constitutes the start of a word and what constitutes the end of the word, once you understand this, if should become obvious how to parse the string correctly.
In Java StringTokenizer is the best way to find the count of words in a string.
String sampleString= "cmd addr data data data data...."
StringTokenizer st = new Tokenizer(sampleString);
st.countTokens();
Hope this will help you :)
In java you can use following code to count words in string
public class WordCounts{
public static void main(String []args){
String text="cmd, addr, data, data, data, data";
String trimmed = text.trim();
int words = trimmed.isEmpty() ? 0 : trimmed.split("\\s+").length;
System.out.println(words);
}
}

Cannot cast from BigDecimal to String in jasper (weird error //$JR_EXPR_ID=18$)

I have a jrxml file which has a field of BigDecimal value and when I try to execute the report. I get an error called " Cannot cast from BigDecimal to String in jasper". Im doing the report using ireport 5.6.0, and I get a weird error like this,
1. Cannot cast from BigDecimal to String
value = (java.lang.String)(((java.math.BigDecimal)variable_variable1.getValue())); //$JR_EXPR_ID=18$
<----------------------------------------------------------------------->
2. Cannot cast from BigDecimal to String
value = (java.lang.String)(((java.math.BigDecimal)variable_variable1.getOldValue())); //$JR_EXPR_ID=18$
<-------------------------------------------------------------------------->
3. Cannot cast from BigDecimal to String
value = (java.lang.String)(((java.math.BigDecimal)variable_variable1.getEstimatedValue())); //$JR_EXPR_ID=18$
<-------------------------------------------------------------------------------->
3 errors
I tried to change the expression class. but nothing seems to work. I need the value to get the sum and also as a field.
You have a field e.g $F{ID} with field class=java.math.BigDecimal.As you want to use this field as a string as well as decimal;
You can create two variables variable1 with variable Class=java.math.BigDecimal with Variable Expression as $F{ID}
and create another variable variable2 with variable Class=java.lang.String with Variable Expression as $F{ID}.toPlainString()
When I removed tag "printWhenExpression" for that text field with error, the error did not show again.

Can a composite format in String.Format() be used to return a substring?

I apologize for what seems to be an exceedingly simple question, but after 4 hours of searching and beating my head against the wall, I'm doubting my sanity.
I need a string format expression that trims a supplied string argument much like Substring(0,1). Although I've never seen this in code, it just seems like it should be possible.
Here's a very basic example of what I'm trying to do:
string ClassCode = "B608H2014"; // sample value from the user's class schedule
string fRoom = "{0:0,2}";
string fGrade = "{0:2,2}";
string fYear = "{0:5,4}";
string Classroom = String.Format(fRoom, ClassCode); // intended result - "B6"
string Gradelevel = String.Format(fGrade, ClassCode); // intended result - "08"
string Schoolyear = String.Format(fYear, ClassCode); // intended result - "2014"
This is a very basic example, but I'm trying to use the String.Format() so I can store the format pattern(s) in the database for each respective DTO property since the class code layouts are not consistent. I can just pass in the formats from the database along with the required properties and it extracts what I need.
Does this make sense?

How can you import data that has hex strings using Mongo import?

I am using mongoimport to import a json file. In one of the fields there is potentially regex expressions that contain hex strings such as:
{ "field1" : "/\x01\x02\x03/" }
I am getting the following error "FailedToParse: Hex escape not supported"
Try using double escape, what you have fails JSON validation.
{ "field1": "/\\x01\\x02\\x03/" }
This passes validation at http://jsonlint.com/
You may have to create a special case in your code for setting and getting this value, there is also an escape notion of \u in JSON which I believe denotes a hex escape but I've never used it before.
From some research I found this handy bit of info and example.
// Store RegExp pattern as a string
// Double backslashes are required to put literal \ characters in the string
var jsonObject = { "regex": "^http:\\/\\/" };

Correct preg_replace format

What is the correct preg_replace format to replace everything (including the brackets) between (xxxxxx). Example have string (not long) link this: AAAAABBBBB (AAAAABBBBB) and all I want is AAAAABBBBB. I need to use preg_replace as the string and it's string length changes.
Thanks
Not sure if I understood your question right, but this should return the string insite the parentheses
$string = "CCCCC (AAAAABBBBB)";
return preg_replace("(.+\(|\))","",$string);
This regex will basically look for any string followed by ( and singular")" and replace them with "".