Modifying Text Nodes in JavaScript

Properties

  • Data
  • Length

Methods

  • appendData(String)
  • deleteData(start, length)
  • insertData(start, string)
  • replaceData(start, length, string)
  • splitText(start)
  • substringData(start, length)

Length

Length property is used to access the length of Text which indicates the number of characters it contains.

Syntax:- TextNode.length

Data

Data property is used to set or return the value of a text node and comment node objects.

Syntax: –
Sets
TextNode.data = value
Returns
TextNode.data

Ex:- TextNode.data = “Hello DevOpsSchool”;

appendData(String)

This method is used to append the passed string to the end of the text node and comment node.

Syntax: –
TextNode.appendData(String);

Ex: –
TextNode.appendData(“Hello DevOpsSchool”);

deleteData(start, length)

This method is used to delete or remove text content from a text node and comment node. It deletes the specified length of characters starting from index defined in start.

Syntax:-
TextNode.deleteData(start, length);

Ex: –
TextNode.deleteData(0, 5);

insertData(start, string)

This method is used to insert text content to text node and comment node. It inserts the value in string, starting at the character index specified in start.

Syntax:-
TextNode.insertData(start, string)

Ex: –
TextNode.insertData(3, “Hello”)

replaceData(start, length, string)

This method is used to replace text content of text node and comment node. It inserts the value in string, starting at the character index specified in start.

Syntax:-
TextNode.replaceData(start, length, string)

Ex: –
TextNode.replaceData(0, 4, “Hello”)

splitText(start)

The splitText(start) method breaks the Text node into two nodes at the specified start index, keeping both nodes in the tree as siblings. It returns right side of the split in a new text node and leaves the left side in the original.

Syntax: –
var rightSide = TextNode.splitText(start)

Ex: –
var rightSide = TextNode.splitText(5)

substringData(start, length)

It returns the part of the current element’s (TextNode and CommentNode) text content from the specified position with the specified length.

Syntax:-
TextNode.substringData(start, length)

Ex: –
TextNode.substringData(2, 5);

Tagged : / /

Which String Functions are present in String?

  • addcslashes — Quote string with slashes in a C style
  • addslashes — Quote string with slashes
  • bin2hex — Convert binary data into hexadecimal representation
  • chop — Alias of rtrim
  • chr — Return a specific character
  • chunk_split — Split a string into smaller chunks
  • convert_cyr_string — Convert from one Cyrillic character set to another
  • convert_uudecode — Decode a uuencoded string
  • convert_uuencode — Uuencode a string
  • count_chars — Return information about characters used in a string
  • crc32 — Calculates the crc32 polynomial of a string
  • crypt — One-way string hashing
  • echo — Output one or more strings
  • explode — Split a string by string
  • fprintf — Write a formatted string to a stream
  • get_html_translation_table — Returns the translation table used by htmlspecialchars and htmlentities
  • hebrev — Convert logical Hebrew text to visual text
  • hebrevc — Convert logical Hebrew text to visual text with newline conversion
  • hex2bin — Decodes a hexadecimally encoded binary string
  • html_entity_decode — Convert all HTML entities to their applicable characters
  • htmlentities — Convert all applicable characters to HTML entities
  • htmlspecialchars_decode — Convert special HTML entities back to characters
  • htmlspecialchars — Convert special characters to HTML entities
  • implode — Join array elements with a string
  • join — Alias of implode
  • lcfirst — Make a string’s first character lowercase
  • levenshtein — Calculate Levenshtein distance between two strings
  • localeconv — Get numeric formatting information
  • ltrim — Strip whitespace (or other characters) from the beginning of a string
  • md5_file — Calculates the md5 hash of a given file
  • md5 — Calculate the md5 hash of a string
  • metaphone — Calculate the metaphone key of a string
  • money_format — Formats a number as a currency string
  • nl_langinfo — Query language and locale information
  • nl2br — Inserts HTML line breaks before all newlines in a string
  • number_format — Format a number with grouped thousands
  • ord — Return ASCII value of character
  • parse_str — Parses the string into variables
  • print — Output a string
  • printf — Output a formatted string
  • quoted_printable_decode — Convert a quoted-printable string to an 8 bit string
  • quoted_printable_encode — Convert a 8 bit string to a quoted-printable string
  • quotemeta — Quote meta characters
  • rtrim — Strip whitespace (or other characters) from the end of a string
  • setlocale — Set locale information
  • sha1_file — Calculate the sha1 hash of a file
  • sha1 — Calculate the sha1 hash of a string
  • similar_text — Calculate the similarity between two strings
  • soundex — Calculate the soundex key of a string
  • sprintf — Return a formatted string
  • sscanf — Parses input from a string according to a format
  • str_getcsv — Parse a CSV string into an array
  • str_ireplace — Case-insensitive version of str_replace.
  • str_pad — Pad a string to a certain length with another string
  • str_repeat — Repeat a string
  • str_replace — Replace all occurrences of the search string with the replacement string
  • str_rot13 — Perform the rot13 transform on a string
  • str_shuffle — Randomly shuffles a string
  • str_split — Convert a string to an array
  • str_word_count — Return information about words used in a string
  • strcasecmp — Binary safe case-insensitive string comparison
  • strchr — Alias of strstr
  • strcmp — Binary safe string comparison
  • strcoll — Locale based string comparison
  • strcspn — Find length of initial segment not matching mask
  • strip_tags — Strip HTML and PHP tags from a string
  • stripcslashes — Un-quote string quoted with addcslashes
  • stripos — Find the position of the first occurrence of a case-insensitive substring in a string
  • stripslashes — Un-quotes a quoted string
  • stristr — Case-insensitive strstr
  • strlen — Get string length
  • strnatcasecmp — Case insensitive string comparisons using a “natural order” algorithm
  • strnatcmp — String comparisons using a “natural order” algorithm
  • strncasecmp — Binary safe case-insensitive string comparison of the first n characters
  • strncmp — Binary safe string comparison of the first n characters
  • strpbrk — Search a string for any of a set of characters
  • strpos — Find the position of the first occurrence of a substring in a string
  • strrchr — Find the last occurrence of a character in a string
  • strrev — Reverse a string
  • strripos — Find the position of the last occurrence of a case-insensitive substring in a string
  • strrpos — Find the position of the last occurrence of a substring in a string
  • strspn — Finds the length of the initial segment of a string consisting entirely of characters contained within a given mask.
  • strstr — Find the first occurrence of a string
  • strtok — Tokenize string
  • strtolower — Make a string lowercase
  • strtoupper — Make a string uppercase
  • strtr — Translate characters or replace substrings
  • substr_compare — Binary safe comparison of two strings from an offset, up to length characters
  • substr_count — Count the number of substring occurrences
  • substr_replace — Replace text within a portion of a string
  • substr — Return part of a string
  • trim — Strip whitespace (or other characters) from the beginning and end of a string
  • ucfirst — Make a string’s first character uppercase
  • ucwords — Uppercase the first character of each word in a string
  • vfprintf — Write a formatted string to a stream
  • vprintf — Output a formatted string
  • vsprintf — Return a formatted string
  • wordwrap — Wraps a string to a given number of characters
Tagged : / / / / / / / /

Tutorial for Double Quoted String

What is Double Quoted string?

In PHP, we can also define a string by wrapping it in a double quote. However, double quote PHP strings will be used to evaluate escape sequences and variables.

How to Create Strings Using Double Quoted with Example.

When compared to single quotes, double quotes are utilised to build more complicated strings.

Variable names can be enclosed in double quotations, and the values of those variables will be displayed.

Consider the following scenario.

Example 1:-

Output:

Hello text within double quote

Example 2:-

Output:

Parse error: syntax error, unexpected 'quote' (T_STRING) in C:\wamp\www\string1.php on line 2

Example 3:-

Output:

Hello text multiple line text within double quoted string 
Using double "quote" with backslash inside double quoted string 
Using escape sequences in double quoted string

Example 4:-

Output:

Number is: 10

Tagged : / / / / / / /

Tutorial for Single Quoted String

What is Single Quoted string?

In PHP, we can make a string by surrounding the content in a single quotation mark. In PHP, it is the simplest way to specify a string.

Escape a literal single quote with a backslash (), and use double backslash to specify a literal backslash (). (\). All other backslash characters, such as r and n, will be output as they are instead of having any special meaning.

How to Create Strings Using Single quotes with Example.

Let’s have a look at the four alternative approaches to writing PHP string functions and manipulating strings in PHP.

Using Single Quotes to Build PHP Strings: The simplest approach to create a string is to use single quotes.

Let’s have a look at a PHP example that generates a simple string.

Example 1:-

Output:

Hello text within single quote

Example 2:-

Output:

Hello text multiple line text within single quoted string 
Using double "quote" directly inside single quoted string 
Using escape sequences \n in single quoted string

Example 3:-

Output:

trying variable $num1 
trying backslash n and backslash t inside single quoted string \n \t 
Using single quote 'my quote' and \backslash

Tagged : / / / / / /

Complete tutorial for String in PHP

What is String?

A PHP string is a collection of characters that can be used to save and manipulate text. PHP only supports a 256-character set, hence it doesn’t support Unicode natively.

The term “string” refers to a group of characters. PHP supports a number of data types, including strings.

Alphanumeric characters are allowed in string variables. When these conditions are met, strings are generated.

  • You create a variable and assign it to string characters.
  • With the echo statement, you can use PHP Strings directly.
  • String functions in PHP are language constructs that aid in the capturing of words.
  • Understanding how PHP strings work and manipulating them can help you become a more successful and productive developer.

In PHP, there are four different ways to specify a string literal.

  1. single quoted
  2. double quoted
  3. heredoc syntax
  4. newdoc syntax (since PHP 5.3)
Tagged : / / / / / /

How many data types How to work are there in JavaScript?

Boolean

Boolean is the built-in object corresponding to the primitive Boolean data type. JavaScript boolean can have one of two values: true or false.

Primitive Values
var primitiveTrue = true;
var primitiveFalse = false;

Boolean Function
var functionTrue = Boolean(true);
var functionFalse = Boolean(flase);

Boolean Constructor
var constructorTrue = new Boolean(true);
var constructorFalse = new Boolean(false);

Boolean

If value parameter is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (“”), the object has an initial value of false.

String

A string is a built-in object corresponding to the primitive string data type.
A string is a group of characters.

String

Primitive
var str = “Hello DevopsSchool”;——————————
var str = „Hello DevopsSchool‟; ——————————String
var str = Hello DevopsSchool;—————————–

Constructor
var str = new String (“Hello DevopsSchool”);——————————
var str = new String („Hello DevopsSchool‟); —————————– Object
var str = new String (Hello DevopsSchool);—————————–

Accessing String

Access String

String Concatenation

Concat Operator

Template Literal/ Template Strings

Template literals are string literals allowing embedded expressions. You can
use multi-line strings and string interpolation features with them.
Template literals are enclosed by the back-tick () character instead of double
or single quotes.

Multiple Line String

String Interpolation

Template literals can contain placeholders. These are indicated by the dollar sign and curly braces
(${expression})

String Methods

  • charAt ( )
  • charCodeAt( )
  • toUpperCase( )
  • toLowerCase ( )
  • Trim( )
  • Replace ( )
  • Split ( )
  • indexOf( )
  • Search ( )
  • Slice ( )
  • Substring( )
  • Substr ()
Tagged : / / / /

List of keywords and reserved words in JavaScript

Keyword or Reserved words

vardeleteforletbrack
supervoidcasedostatic
functionnewswitchwhileinterface
catchelseifpackagefinally
thiswithclassenumdefault
implementsprivatethrowtiledtypeof
constexportimportprotectedreturn
truecontinueextendsin
instanceof
publictrydebuggerfalse

In JavaScript, we do not need to specify the type of the variable because it is dynamically used by the JavaScript engine.
We can use var data type. It can hold any type of data like String, Number, Boolean, etc.

Primitive Data Type Non-Primitive Data type

String Object
Number Array
Boolean RegExp
Undefine

Tagged : / / / /

String manipulation exercise

mnanjala created the topic: String manipulation exercise

Hi Guys,

Say there is a text file inject.dat, it has 5 entries as below
PKGNAME=Project-debug
OLD_VERSION=1.0.0
NEW_VERSION=1.0.1
PRODUCT NUMBER=1
RELASE DATE=11/11/2011

How we could write shell script so I can read each line replace everything after “=”sign, with new value, example run
./inject.sh $PKGNAME $OLD_VERSION $NEW_VERSION $PRODUCT_NUMBER $RELEASE_DATE , so it should update the inject.dat with newer value what ever I give as parameter? Any idea?

Hint getopts.

rajeshkumar replied the topic: Re: String manipulation exercise
Hi praveen,

getopts is the best way to implement. I could not get much time to look into it. However, please find my quick code to implement the same. Please change the file name as needed. if time permits, tomorrow i will tell you more better way.

for line in $(< raj.txt) do if [ $# -eq 5 ] then case $line in PKGNAME=$1) eval $line ;; OLD_VERSION=$2) eval $line ;; NEW_VERSION=$3) eval $line ;; PRODUCT_NUMBER=$4) eval $line ;; RELASE_DATE=$5) eval $line ;; *) ;; esac fi done echo PKGNAME=$1 >> temp.txt
echo OLD_VERSION=$2 >> temp.txt
echo NEW_VERSION=$3 >> temp.txt
echo PRODUCT_NUMBER=$4 >> temp.txt
echo RELASE_DATE=$5 >> temp.txt

mv temp.txt raj2.txt

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :