# Primitives

[Primitive Types](#primitive-types): [Boolean](#boolean), [Integer](#integer), [Double](#double), [Float](#float), [Long](#long), [String](#string)

## Boolean

The most basic datatype is the Boolean value. It simply return true or false.&#x20;

<pre class="language-javascript"><code class="lang-javascript"><strong>var Condition = false; // Condition now has a boolean value of false
</strong></code></pre>

The Boolean Class includes the following method:

* `static parseBoolean(text: String): Boolean`

```javascript
// Sample input string
var text = "true";

// Using parseBoolean to convert the string to a Boolean
var result = Boolean.parseBoolean(text);

// Now 'result' will contain the Boolean value 'true'
```

This static method takes a string as input and returns a Boolean object representing the parsed boolean value.

***

## Integer

Another essential primitive type is the integer. It allows it to hold values from approximately -2,147,483,648 to 2,147,483,647. It can represent numbers with up to 10 digits, both positive and negative.

<pre class="language-typescript"><code class="lang-typescript"><strong>var num = 2023; // num has an integer value of 2023
</strong></code></pre>

The Integer Class includes the following methods:

* `static parseInt(text: String): Integer`

This static method takes a string as input and returns an `Integer` object representing the parsed integer value.

```typescript
// Sample input string
var text = "42";

// Using parseInt to parse the string to an Integer
var parsedInt = Integer.parseInt(text);

// Now 'parsedInt' contains the Integer value 42
```

* `static sum(num1: Integer, num2: Integer): Integer`

This static method takes two `Integer` objects and returns their sum as an `Integer`.

```typescript
// Sample Integer values
var num1 = Integer.valueOf(5);
var num2 = Integer.valueOf(7);

// Using sum to calculate the sum of num1 and num2
var result = Integer.sum(num1, num2);

// Now 'result' contains the Integer value 12 (5 + 7)
```

***

## Double

The double data type is a numeric type designed to store real numbers with decimal precision. It is a fundamental data type suitable for a wide range of calculations, making it a crucial component for working with decimal values in B2Data.

```javascript
// Double data type usage
var pi = 3.14159265359;
var radius = 5.0;
var area = pi * (radius * radius);
```

The Double Class includes the following methods:

* `static parseDouble(text: String): Double`

This static method takes a string as input and returns a `Double` object representing the parsed double value.

```javascript
// Sample input string
var text = "4.2";

// Using parseDoubleto parse the string to a Double
var parseDouble= Double.parseDouble(text);

// Now 'parseDouble' contains the Double value 4.2
```

***

## Long

The long data type is a numeric type specifically designed to handle large integer values with extended range. It is invaluable when you need to work with integers beyond the capabilities of standard integer types.&#x20;

```javascript
var bigNumber = 12345678912; 
// bigNumber now has a long value of 12345678912
```

The Long Class includes the following methods:

* `static parseLong(text: String): Long`

This static method takes a string as input and returns a `Long` object representing the parsed long integer value.

```javascript
// Sample input string
var text = "123456789";

// Using parseLongto parse the string to a Long
var parseLong = Long.parseLong(text);

// Now 'parseLong' contains the Long value 123456789
```

***

## Float

The float data type is a numeric type optimized for representing real numbers with decimal precision. While similar to the double data type, it typically consumes less memory and provides sufficient accuracy for many computations.&#x20;

The Float Class includes the following methods:

* `static parseFloat(text: String): Float`

This static method takes a string as input and returns a `Float` object representing the parsed float value.

```javascript
// Sample input string
var text = "2.159";

// Using parseFloatto parse the string to a Float
var parseFloat = Float.parseFloat(text);

// Now 'parseFloat' contains the Float value 2.159
```

***

## String

In our programming language, the string data type serves as a fundamental way to handle sequences of characters. It is essential for tasks like representing text, processing user input, and managing textual data within your applications. Strings provide a versatile and indispensable tool for working with character-based information.

```javascript
var company = "NAZDAQ";
// Now 'Company' contains the string value "NAZDAQ"
```

* `toString(): String`

This method returns a string representation of the `String` object.

```typescript
var text = "Hello, World!";
string result = text.toString();
// 'result' now contains the string "Hello, World!"
```

* `isEmpty(): Boolean`

Returns `true` if the string is empty, otherwise `false`.

```typescript
string emptyString = "";
string nonEmptyString = "B2DATA";

bool isEmpty1 = emptyString.isEmpty(); // Returns true
bool isEmpty2 = nonEmptyString.isEmpty(); // Returns false
```

* `charAt(pos: Integer): String`

Returns the character at the specified index.

```typescript
var text = "Hello";
varcharacter = text.charAt(2);
// 'character' now contains the string "l"
```

* `matches(regexp: String): RegExpMatchArray | null`

This method matches the string with a regular expression and returns a boolean value to indicate whether the string matches the pattern. Here are two examples showcasing the use of the `matches` method for validation:

**Example I**: Email validation&#x20;

In this example, we use the `matches` method to validate an email address by checking if it matches a common email pattern.

```javascript
// Email to be validated
var email = "example@email.com";

// Define a regular expression pattern for basic email validation
var emailPattern = "^[A-Za-z0-9+_.-]+@(.+)$";

// Use the matches() method to check if the email matches the pattern
var isValid = email.matches(emailPattern); 

return isValid; // true (if email is valid according to the pattern)
```

**Example II**: File Name Validation

In this example, we use the `matches` method to validate a file name by checking if it matches a basic file name pattern.

```javascript
// File name to be validated
var fileName = "example_file.txt";

// Define a regular expression pattern for basic file name validation
var filePattern = "^[a-zA-Z0-9_\\-\\.]+$";

// Use the matches() method to check if the file name matches the pattern
var isValid = fileName.matches(filePattern);

return isValid; // true (if fileName is valid according to the pattern)
```

* `replace(searchValue: String, replaceValue: String): String`

This method replaces text in the string using a search string.

```javascript
// Original string
var originalString = "NAZDAQ just introducted its latest product: B2Output";

// Use the replace method to replace "B2Output" with "B2Data"
var newString = originalString.replace("B2Output", "B2Data");

return newString;
// 'newString' contains: "NAZDAQ just introducted its latest product: B2Data."
```

* `replaceAll(searchValue: String, replacer: (match: String) => String): String`

This method replaces all substrings in a string that match a specified regular expression with a given replacement string.

**Example:** Sanitize Data

In this example, we use the `replaceAll` method to protect customer privacy in a business context. The original customer data contains sensitive information, including customer IDs and email addresses. We apply two consecutive method calls to achieve this:

```typescript
var customerData = "Customer ID: 12345, Email: john.doe@email.com, 
                                Phone: 555-555-5555";

// Using replaceAll to replace customer IDs and email addresses with "X"
var sanitizedData = customerData.replaceAll("\\d+", "X").replaceAll(
                        "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}", "X");

return "Sanitized Data: " + sanitizedData;
// Sanitized Data: Customer ID: X, Email: X, Phone: X-X-X
```

* `contains(str: String): Boolean`

Checks if the string contains a specific substring.

<pre class="language-javascript"><code class="lang-javascript"><strong>// Create a string
</strong>var text = "Hello, World!";

// Check if it contains "World"
var containsWorld = text.contains("World"); // Returns true
var containsUniverse = text.contains("Universe"); // Returns false
</code></pre>

* `split(separator: String, limit: Integer): Array<String>`

Splits the string into substrings using the specified separator.

Example: Suppose you have a CSV (Comma-Separated Values) string representing sales data, and you want to split it into an array to work with the individual sales records:

```typescript
// CSV data containing sales records separated by newlines
var csvData = "2023-01-05,ProductA,500.00\n2023-01-10,
                                ProductB,750.00\n2023-01-15,ProductC,300.00";

// Use the split method to split the CSV data into an array of sales records
var salesRecords = csvData.split('\n');

// Now, salesRecords is an array where each element represents a single sales record.

// For example, salesRecords[0] contains "2023-01-05,ProductA,500.00", 
// salesRecords[1] contains "2023-01-10,ProductB,750.00", and so on.

// The '\n' was used to split the CSV data, resulting in an array of sales records.

return salesRecords;
```

* `toLowerCase(): String`

Converts all alphabetic characters to lowercase.

```javascript
// Create a string
var text = "Hello, World!";

// Convert all alphabetic characters to lowercase
var lowerCaseText = text.toLowerCase();
// 'lowerCaseText' contains the string "hello, world!"
```

* `toUpperCase(): String`

Converts all alphabetic characters to uppercase.

```typescript
// Create a string
var text = "Hello, World!";

// Convert all alphabetic characters to uppercase
var upperCaseText = text.toUpperCase();
// 'upperCaseText' contains the string "HELLO, WORLD!"
```

* `trim(): String`

Removes leading and trailing white space and line terminator characters.

```javascript
// Create a string with leading and trailing whitespace
var fullname = "   Hello, World!   ";

// Remove leading and trailing whitespace and line terminator characters
var trimmedText = text.trim();
// 'trimmedText' contains the string "Hello, World!"
```

Learn about more [advanced Data Types](https://docs.b2winsuite.com/scripting/types-and-objects/arrays-and-maps) such as array and list:
