I recife a JSON with some values for example 399.00&euro but I need to convert the text &euro to € in this example. But when I receive something else like 399.00&USD I have to convert the text to $.
How can I convert the text after the number automatically?
you can use replaceAll() if you are reciving an String as response and them convert to JSONObject:
String responseAux= response.replaceAll("&euro","€");
Or do it when you get the String of the json object using replace():
String value= jsonObject.getString(Constants.JSON_NAME).replace("&euro","€");
Try this:
/**
*#param {String} separator e.g: &
*#param {Array} symbols e.g: [{key:'euro', value:'€'}, {key:'USD', value:'$'}]
*#param {String} string to inspection
*#return {String}
/**
var convertCurrency=function(separator, symbols, value){
if(_.isArray(symbols) && !_.isEmpty(symbols)){
for(var i=0;i<symbols.length;i++){
var symbol=symbols[i];
var replace=separator.concat(symbol.key);
value=value.replaceAll(replace, symbol.value);
}
}
return value;
};
Related
how could I convert a string in binary to string again in react native?
Ex: 01010 to Hello
I have the code to convert string to binary
Ex: text.split('').map(l => l.charCodeAt(0).toString(2)).join(' '),
let txt="Hello".split('').map(l => l.charCodeAt(0).toString(2)).join(' ')
let s = txt.split(" ").map(w=> String.fromCharCode(parseInt(w,2)))
console.log(s.join(""))
Just convert binary string back to integer and map those values to character using fromCharCode
To convert binary string to sting you can use parseInt and String.fromCharCode
parseInt converts strings to number. First argument is string value and the second argument is the base value in which the string representation is.
let num = parseInt(binaryStr, 2);
String.fromCharCode converts character code to matching string.
let str = String.fromCharCode(65);
Complete code with example:
const text = 'Hello'
let binaryStr = text.split('').map(l => l.charCodeAt(0).toString(2)).join(' ');
console.log('Binary string:', binaryStr);
//To convert binary to string
let strVal = binaryStr.split(' ').map(l => String.fromCharCode(parseInt(l, 2))).join('');
console.log('String:', strVal);
Output of above code:
Binary string: 1001000 1100101 1101100 1101100 1101111
String: Hello
How can a textField were a comma is inserted be Formated so the inserted Decimal value example 10,05$ can be converted to a decimal(Point) 10.05$?
private static let Intrest = "Intrest"
private static let Repayment = "Repayment"
var loan: Double {
let intrest = Double (Intrest)
let repayment = Double (Repayment)
let Loan = Intrest + Repayment
return Loan
}
var body: some View {
VStack{
HStack{
Text ("Repayment %")
Spacer()
TextField("2 %", text: $Repayment)
.keyboardType(.decimalPad)
}
VStack{
HStack{
Text ("Loan / month")
}
TextField("2 %", text: $Loan)
.keyboardType(.decimalPad)
}
}
The problem only occurs because in many countries mostly comma is used which breaks the calculation.
Thanks
You need to make sure you convert your string into a double with point and not comma.
This one should help.
Swift playground - How to convert a string with comma to a string with decimal
I suggest you read Textfield not updating and use custom string binding with double back storage.
Specifically read the Update there :-)
Complete example usage of custom string binding with backend double number is here https://gist.github.com/op183/24607b6309f7d1ea8f3aaf02d17e4ed3
I would like to use this symbol in my React Native project.
I tried using the Unicode encoding like this:
var arrow = "U+0279C";
And in the JSX:
<Text>
{arrow}
</Text>
However, this just displays the encoding literally: U+0279C.
So any idea how can I use a symbol in JSX?
You should use the HTML code for the symbol.
<Text>
➜
</Text>
As described in notes below.. (important, quotes don't seem to work)...
Just to clarify: <Text>➜</Text> will work, but <Text>{ '➜' }</Text> will not.
Use this functions for symbols in this format: & # 1 7 4 ;
/**
* replaces /$#d\+/ symbol with actual symbols in the given string
*
* Returns given string with symbol code replaced with actual symbol
*
* #param {string} name
*/
export function convertSymbolsFromCode(name = '') {
let final = null;
if (name) {
const val = name.match(/&#\d+;/) ? name.match(/&#\d+;/)[0] : false; // need to check whether it is an actual symbol code
if (val) {
const num = val.match(/\d+;/) ? val.match(/\d+;/)[0] : false; // if symbol, then get numeric code
if (num) {
final = num.replace(/;/g, '');
}
}
if (final) {
name = name.replace(/&#\d+;/g, String.fromCharCode(final));
}
}
return name;
}
USAGE
<Text>
{convertSymbolsFromCode(this.state.unicode)}
</Text>
The provided answer did not work for me, since I was dynamically retrieving unicode hex codes from an API. I had to pass them as JavaScript into the react-native jsx code.
The following answer worked for me:
Concatenate unicode and variable
I used String.fromCodePoint(parseInt(unicode, 16)) and it worked.
Example:
const unicode = unicodeHexValueFromApi //This value equals "05D2"
return(<Text>{String.fromCodePoint(parseInt(unicode, 16))}</Text>)
<Text>{"Any character"}</Text>
This worked for me.
I'm trying to make assertion that the random text entered in one field appears on next page (confirmation)
I do it like this
When I fill in "edit-title" with random value of length "8"
/**
* Fills in form field with specified id|name|label|value with random string
* Example: And I fill in "bwayne" with random value of length "length"
*
* #When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with random value of length "(?P<length>(?:[^"]|\\")*)"$/
*/
public function fillFieldWithRandomValue($field, $length)
{
$field = $this->fixStepArgument($field);
$value = $this->generateRandomString($length);
$this->getSession()->getPage()->fillField($field, $value);
}
Than I want to make assertion - something like this:
Then I should see text matching "<RANDOM VALUE ENTERED IN THE PREVIOUS STEP>"
is it possible?
UPDATE:
But how would it look like with setters and getters if i want to use a generateRandomString method multiple times and then get the values of this methods one after another? DO I have to make variables and functions for every test step? like this:
When I fill in "x" with random value of length "8"
And I fill in "y" with random value of length "12"
And I go to other page
Then I should see text matching "VALUE ENTERED TO X"
And I should see text matching "VALUE ENTERED TO Y"
You can create a property and set it in the previous step. And use it in the next one, but assert it if it has value.
Also it would be nice and readable to define that property with proper visibility type
/**
* #var string
*/
private randomString;
/**
* Fills in form field with specified id|name|label|value with random string
* Example: And I fill in "bwayne" with random value of length "length"
*
* #When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with random value of length "(?P<length>(?:[^"]|\\")*)"$/
*/
public function fillFieldWithRandomValue($field, $length)
{
$field = $this->fixStepArgument($field);
$this->randomString = $this->generateRandomString($length);
$this->getSession()->getPage()->fillField($field, $this->randomString);
}
/**
*
* #Then /^(?:|I )should see that page contains random generated text$/
*/
public function assertPageContainsRandomGeneratedText()
{
//Assertion from phpunit
$this->assertNotNull($this->randomString);
$this->assertPageContainsText($this->randomString);
}
NOTE: Depending on your behat setup - assertion from phpunit might not work.
Since you will will call the generateRandomString method in multiple places then you should also have a method for getting this value like getRandomString like setters and getters.
My recommendation would be to have a class with related methods that handle all the data and not saving in variable in every place you will use data, generate+save and read from the same place anywhere you need.
Tip: You could be more flexible about the step definition and have a default length for the random string in case one one not provided.
High level example:
class Data
{
public static $data = array();
public static function generateRandomString($length = null, $name = null)
{
if ($name = null) {
$name = 'random';
};
if ($length = null) {
$length = 8;
};
// generate string like $string =
return self::$data[$name] = $string;
}
public static function getString($name = null)
{
if ($name = null) {
$name = 'random';
};
// exception handling
if (array_key_exists($name, self::$data) === false) {
return null;
}
return self::$data[$name];
}
}
In context:
/**
* #Then /^I fill in "x" with random value as (.*?)( and length (\d+))?$/
*/
public function iFillInWithRandomValue($selector, $name, $length = null){
$string = Data::generateRandomString($length, $name);
// fill method
}
/**
* #Then /^I should see text matching "first name"$/
*/
public function iShouldSeeTextMatching($variableName){
$string = Data::getString($variableName);
// assert/check method
}
This is high level example, you might need to do some adjustments.
If you have the validation in the same class then you can also have all these in the same class, meaning generateRandomString and getString in the same class with the steps.
If the title isn't clear; I want to be able to select any character from textbox without making some complex loops-dependent code (I can do that one). For example, let's consider this text is entered in a textbox:
hello user!
I want some syntax when I tell to get me the index 1's value, it gives me "h", for index 5 = "o"... etc
So, anyone knows what's the right syntax, please help!
string can be directly indexed without any special code.
//from metadata
public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>
{
....
// Summary:
// Gets the character at a specified character position in the current System.String
// object.
//
// Parameters:
// index:
// A character position in the current string.
//
// Returns:
// A Unicode character.
//
// Exceptions:
// System.IndexOutOfRangeException:
// index is greater than or equal to the length of this object or less than
// zero.
public char this[int index] { get; }
....
}
dim str = "hello";
dim hchar = str(0);
dim echar = str(1);
dim lchar = str(2);
ect
Dim x As String = "Hello"
Console.Write(x.IndexOf("e")) 'Would return the position
Console.Write(x(x.IndexOf("e"))) 'Would return the character based upon the position
Console.Write(x(1)) 'Returns the character based at position 1 of the string
You can remove the Console.Write if you are using a WinForm.
TextBox1.Text = x(x.IndexOf("e"))
This should work.
Dim orig = "hello user!"
Dim res = Enumerable.Range(0,orig.Length).[Select](Function(i) orig.Substring(i,1))
So then you can do:
Dim x = res(0) 'x = "h"