String
- C
- C++
- C#
- Java
- Rust
- Go
- Javascript
- Typescript
- Python
// ------------------------------------
// Declaration & Initialization
// ------------------------------------
// No native string type
// In C, strings are an array of characters
// This is a string with lenght = 20. In C you have to account for the '\0' character.
char variable_name[21]; // 20 + 1 from the '\0' character.
char variable_name[81] = "Hi"; // Doesnt need to fill it all at once
char variable_name[81] = {'H','i'};
// No need to include '\0' here. The compiler inserts it at the end of the array for us.
char variable_name[] = "Some string";
char *variable_name = "Some string";
// Const Declaration
const char MY_CONSTANT[] = "Something";
const char *MY_CONSTANT = "Something";
// ------------------------------------
// Direct Access (Array-Like)
// ------------------------------------
char myString[] = "Hi";
myString[1] = 'o';
// ------------------------------------
// Size / Length
// ------------------------------------
#include<string.h> // For strlen(), strnlen_s(), strnlen()
int length = strlen(myString); // Not safe. Undefined Behavior if "myString" is missing null-terminated byte
// strnlen_s() - Since C11
// strnlen_s takes a parameter for the maximum number of characters to scan.
// This way, it won't overflow the buffer you provide it if there's no trailing null byte
int length = strnlen_s(myString, sizeof myString); // Yet, Might not work in every compiler
// strnlen()
int length = strnlen(myString, sizeof myString);
// ------------------------------------
// Trim
// ------------------------------------
// No Native Support.
// Create a helper function or use a library.
// Example trimming leading spaces in-place:
#include <ctype.h> // For isspace()
#include <string.h> // For memmove()
void trim_left(char *value)
{
char *start = value;
while (isspace((unsigned char)*start))
{
start++;
}
memmove(value, start, strlen(start) + 1);
}
// ------------------------------------
// Is Null Or Empty
// ------------------------------------
bool isNullOrEmpty = myString == NULL || myString[0] == '\0';
// ------------------------------------
// Transform
// ------------------------------------
#include <ctype.h> // For toupper(), tolower()
for (int i = 0; myString[i] != '\0'; i++)
{
myString[i] = (char)toupper((unsigned char)myString[i]);
}
// ------------------------------------
// Compare
// ------------------------------------
#include<string.h> // For strcmp(), strncmp()
int result = strcmp(string1, string2); // Beware, Undefined Behavior if any string is missing null-terminated byte
// result == 0, indicates string1 is equal to string2.
// result < 0, indicates string1 is less than string2.
// result > 0, indicates string2 is less than string1.
int result = strncmp(string1, string2, count); // count = maximum number of characters to compare.
// From StackOverflow:
// Using strncmp you can limit the search, so that it doesn't reach non-accessible memory.
// But, from that it should not be concluded that strcmp is insecure to use. Both the
// functions works well in the way they are intended to work.
//
// strncmp does not have "advantages over strcmp"; rather they solve different problems.
// strcmp is for determining if two strings are equal (and if not, possibly how to
// order/sort them with respect to each other). strncmp is (mainly) for determining
// whether a string begins with a particular prefix. For example:
if (strncmp(str, "--option=", 9)==0) { /**/ }
// ------------------------------------
// Copy / Clone
// ------------------------------------
#include<string.h> // For strcpy(), strncpy(), strncpy_s()
char* originalSring = "Hi";
char* copyString[10];
strcpy(copyString, originalSring); // Not safe. Does not specify the size of the destination array, so buffer overrun is often a risk
strncpy(copyString, originalSring, count); // Not safe. "strncpy()" does not guarantee that the destination string will be NULL terminated.
// count = maximum number of characters to copy. count Could also be "sizeof copyString"
// strncpy_s - Since C11
strncpy_s(copyString, sizeof copyString, originalSring, (sizeof copyString)-1); // strncpy_s unlike strncpy is a null terminated string function
// ------------------------------------
// Concatenation
// ------------------------------------
#include<string.h> // For strcat(), strcat_s(), strncat_s()
strcat(mainString, addString); // Not Safe
// The behavior is undefined if the destination array is not large enough for the contents
// of both src and dest and the terminating null character.
// The behavior is undefined if the strings overlap.
// The behavior is undefined if either dest or src is not a pointer to a null-terminated
// byte string.
// strcat_s - Since C11
strcat_s(mainString, sizeof mainString, addString);
//strncat_s - Since C11
strncat_s(mainString, sizeof mainString, addString, count);
// ------------------------------------
// String Interpolation
// ------------------------------------
// Not Available
// ------------------------------------
// Print / Output
// ------------------------------------
#include <stdio.h> // For printf
// Main structure omitted
printf("Hello World\n"); // '\n' is the new line character
printf("Printing Integer: %d\n", 10);
printf("Printing Float: %f\n", 10.5);
printf("Printing Double: %lf\n", 20.5);
printf("Printing Character: %c\n", 'a'); // Single quote
printf("Printing String: %s\n", "my string");
printf("Printing Memory Address: %p\n", &variable_name);
printf("Printing Memory Address: %p\n", pointer_to_variable);
printf("Printing Octal: %o\n", 2567);
printf("Printing Hexadecimal (Letter in small letters): %x\n", 2567);
printf("Printing Hexadecimal (Letter in capital letters): %X\n", 2567);
printf("%s: %d %f", "More Printing", 30, 50.2);
// ------------------------------------
// Advanced Formating
// ------------------------------------
// The syntax template for easy reference:
// %[flags][width][.precision][type_character]
// Both "width" and/or "precision" numbers can be replaced by "*" then,
// an additional integer value argument must be placed preceding the argument that has to be formatted.
/*
Flags:
- Left justify.
0 Field is padded with 0's instead of blanks.
+ Sign of number always O/P.
blank Positive values begin with a blank.
# Various uses:
%#o (Octal) 0 prefix inserted.
%#x (Hex) 0x prefix added to non-zero values.
%#X (Hex) 0X prefix added to non-zero values.
%#e Always show the decimal point.
%#E Always show the decimal point.
%#f Always show the decimal point.
%#g Always show the decimal point trailing zeros not removed.
%#G Always show the decimal point trailing zeros not removed.
*/
// Width & Right Align
// %[width][type_character]
printf("%5d", 7); // length of 5 digits Ex.: " 7"
printf("%20s !\n", "hello World"); //" hello World !"
// Floating Point Precision
// %[.precision][type_character]
printf("%.2f\n", 7.0000); // 7.00
// Can also be combined with Width
// %[width][.precision][type_character]
printf("%8.2f\n", 7.0); // Ex.: " 7.00"
printf("%*.*f\n", 8, 2, 7.0); // same
// Left-justifying
// You can use the "-" flag
printf("%-5d", 7); // length of 5 digits Ex.: "7 "
printf("%-20s !\n", "hello World"); // "hello World !"
// Integer leading zero-fill
// You can use the "0" flag to force the number to be padded with 0s
// %[flags][width][type_character]
printf("%05d", 7); // Ex.: "00007"
printf("%0*d", 5, 7); // same
// Positional Arguments (Re-Ordering the Arguments)
// Using %[[order]$][type_character]
printf("%2$s , %1$s\n", "First Argument", "Second Argument"); // Will print: "Second Argument , First Argument"
More Info:
- https://codeforwin.org/2015/05/list-of-all-format-specifiers-in-c-programming.html
- https://en.cppreference.com/w/c/string/byte/strlen
- https://en.cppreference.com/w/c/string/byte/strncpy
- https://stackoverflow.com/questions/30190460/advantages-of-strncmp-over-strcmp
- https://en.cppreference.com/w/c/string/byte/strcat
- https://en.cppreference.com/w/c/string/byte/strncat
- https://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/format.html
- http://www.cplusplus.com/reference/cstdio/printf/
- https://stackoverflow.com/questions/6322540/how-do-positional-arguments-like-1-work-with-printf
// ------------------------------------
// Declaration & Initialization
// ------------------------------------
#include <string> // For std::string
#include <wstring> // For std::wstring (Alternative) (wide string type)
// Differents mode of initialization
// Copy
std::string variable_name = "Another String";
// Direct
std::string variable_name( "Another String" );
// Uniform Initialization (since C++11)
// Also known as Brace Initialization or List Initialization
std::string variable_name{ "Another String" };
// Const Declaration
/*
Any variable that should not be modifiable after initialization
and whose initializer is not known at compile-time should be
declared as const.
*/
const std::string variable_name = "Another String";
std::string const variable_name = "Another String";
/*
Any variable that should not be modifiable after initialization
and whose initializer is known at compile-time should be
declared as constexpr.
*/
constexpr std::string MY_CONSTANT = "Another String"; // C++11
// ------------------------------------
// Direct Access (Array-Like)
// ------------------------------------
char character = myString[0]; // No bounds-checking
char character = myString.at(0); // with bounds-checking
// ------------------------------------
// Size / Length
// ------------------------------------
std::size_t length = myString.size();
std::size_t length = myString.length();
// ------------------------------------
// Trim
// ------------------------------------
// Not Native
// Code from StackOverflow (Link in More Info)
#include <string>
const std::string WHITESPACES( " \f\n\r\t\v" );
void trimRight( std::string& str, const std::string& trimChars = WHITESPACES )
{
std::string::size_type pos = str.find_last_not_of( trimChars );
str.erase( pos + 1 );
}
void trimLeft( std::string& str, const std::string& trimChars = WHITESPACES )
{
std::string::size_type pos = str.find_first_not_of( trimChars );
str.erase( 0, pos );
}
void trim( std::string& str, const std::string& trimChars = WHITESPACES )
{
trimRight( str, trimChars );
trimLeft( str, trimChars );
}
// ------------------------------------
// Is Null Or Empty
// ------------------------------------
// std::string isn't a pointer, you can't assign null to it
// If you need to distinguish between a string that has no characters
// and a string that has no value, you can use a std::string* and set it to nullptr
// Remember to trim before
myString.size() == 0
myString.empty()
// ------------------------------------
// Transform
// ------------------------------------
#include <algorithm> // For std::transform
#include <cctype> // For std::toupper, std::tolower
std::transform(myString.begin(), myString.end(), myString.begin(),
[](unsigned char character) { return std::toupper(character); });
std::transform(myString.begin(), myString.end(), myString.begin(),
[](unsigned char character) { return std::tolower(character); });
// ------------------------------------
// Compare
// ------------------------------------
bool isEqual = myString == otherString;
int result = myString.compare(otherString);
// ------------------------------------
// Copy / Clone
// ------------------------------------
std::string copy = myString;
std::string copy {myString};
// ------------------------------------
// Concatenation
// ------------------------------------
std::string result = firstString + secondString;
firstString += secondString;
firstString.append(secondString);
// ------------------------------------
// String Interpolation
// ------------------------------------
// Not Yet Available
// ------------------------------------
// Print / Output
// ------------------------------------
#include <iostream> // For std::cout
// Main structure omitted
std::cout << "Hello world!\n"; // '\n' is the new line character
std::cout << "Hello world!" << std::endl; // std::endl prints a newline character AND flushes the output buffer
std::cout << "Hello world!" << '\n' << std::flush; // same as std::endl
std::cout << 10 << std::endl;
std::cout << "Printing Integer: " << 10 << '\n';
std::cout << "Printing Any Number: " << 20.5 << '\n';
std::cout << "Printing An Address: " << &variable_name << '\n';
std::cout << "Printing An Address: " << pointer_to_variable << '\n';
std::cout << "Printing Octal: "<< std::oct << 2567 << '\n';
std::cout << "Printing Hexadecimal (Letter in small letters): "<< std::hex << 2567 << '\n'; // a07
std::cout << "Printing Hexadecimal (Letter in capital letters): "<< std::hex << std::uppercase << 2567 << '\n'; // A07
std::cout << std::nouppercase << std::dec; // Resetting to default
std::cout << "More Printing " << 30 << ", " << 50.2 << '\n';
// ------------------------------------
// Advanced Formating
// ------------------------------------
// Width & Right Align
// std::cout.width()
std::cout.width(5); // No need to reset the width
std::cout << 7 << '\n'; // " 7"
std::cout.fill(' '); // No need to reset the fill
std::cout.width(20); // No need to reset the width
std::cout << "Hello World !" << '\n'; // " Hello World !"
// std::setw()
#include <iomanip> // For std::setw()
std::cout << std::setw(5) << 7 << '\n'; // No need to reset the width
// Floating Point Precision
// std::cout.precision(n);
std::cout.precision(3); // precision will be set to 3 until you change it
std::cout << 70.12345 << '\n'; // will print "70.1"
// std::setprecision(n);
#include <iomanip> // For std::setprecision(n)
std::cout << std::setprecision(3) << 70.12345 << '\n'; // will print "70.1"
// precision will be set to 3 until you change it
// In case you and to fill with zeros when the number is
// smaller than the precision, you need the
std::cout.precision(13);
std::cout << std::fixed << 70.12345 << '\n';
std::cout << std::defaultfloat; // To reset (Remove Fixed)
// Left-justifying
std::cout.fill(' '); // No need to reset the fill
std::cout.width(20); // No need to reset the width
std::cout << std::left << "Hello World";
std::cout << " !" << '\n'; // "Hello World !"
// Integer leading zero-fill
// std::cout.fill()
std::cout.fill('0'); // No need to reset the fill
std::cout.width(5); // No need to reset the width
std::cout << 7 << '\n';
// Positional Arguments (Re-Ordering the Arguments)
// No Native Support with iostreams.
// Use std::format with explicit argument order if supported by the implementation. (Since C++20)
std::string result = std::format("{1} , {0}", "First Argument", "Second Argument");
More Info:
- https://stackoverflow.com/a/402918/13227260
- https://utf8everywhere.org/
- https://stackoverflow.com/questions/213907/c-stdendl-vs-n
- http://demin.ws/blog/english/2012/04/25/endl-vs-new-line/
- https://www.learncpp.com/cpp-tutorial/introduction-to-iostream-cout-cin-and-endl/
- http://www.cplusplus.com/forum/windows/51591/
- https://www.geeksforgeeks.org/using-namespace-std-considered-bad-practice/
- https://www.learncpp.com/cpp-tutorial/const-constexpr-and-symbolic-constants/
- https://www.learncpp.com/cpp-tutorial/variable-assignment-and-initialization/
- https://stackoverflow.com/questions/3903587/how-to-check-if-a-stdstring-is-set-or-not
- https://stackoverflow.com/questions/479080/trim-is-not-part-of-the-standard-c-c-library
// ------------------------------------
// Declaration & Initialization
// ------------------------------------
string variableName = "Some string";
string variableName = @"C:\Some\Path";
string variableName = """
Some string
with multiple lines
"""; // Raw string literals (Since C# 11)
// Const Declaration
const string MY_CONSTANT = "Some string";
readonly string myReadonlyString = "Some string";
// ------------------------------------
// Direct Access (Array-Like)
// ------------------------------------
char character = variableName[0];
// ------------------------------------
// Size / Length
// ------------------------------------
int length = variableName.Length;
// ------------------------------------
// Trim
// ------------------------------------
string trimmed = variableName.Trim();
string trimmedStart = variableName.TrimStart();
string trimmedEnd = variableName.TrimEnd();
// ------------------------------------
// Is Null Or Empty
// ------------------------------------
bool isNullOrEmpty = string.IsNullOrEmpty(variableName);
bool isNullOrWhiteSpace = string.IsNullOrWhiteSpace(variableName);
// ------------------------------------
// Transform
// ------------------------------------
string upper = variableName.ToUpper();
string lower = variableName.ToLower();
// ------------------------------------
// Compare
// ------------------------------------
bool isEqual = variableName == otherString;
bool isEqual = string.Equals(variableName, otherString, StringComparison.Ordinal);
int result = string.Compare(variableName, otherString, StringComparison.Ordinal);
// ------------------------------------
// Copy / Clone
// ------------------------------------
// Strings are immutable.
string copy = variableName;
string copy = string.Copy(variableName); // Obsolete in .NET Core 3.0+
// ------------------------------------
// Concatenation
// ------------------------------------
string result = firstString + secondString;
string result = string.Concat(firstString, secondString);
string result = string.Join(", ", values);
// ------------------------------------
// String Interpolation
// ------------------------------------
// Example 1
string name = "Mark";
var date = DateTime.Now;
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// Example 2
string name = "Horace";
int age = 34;
Console.WriteLine($"{name} is {age} year{(age == 1 ? "" : "s")} old.");
// ------------------------------------
// Print / Output (Old Format)
// ------------------------------------
using System; // For Console.Write and Console.WriteLine
// Main structure omitted
Console.Write("Hello World\n"); // '\n' is the new line character
Console.WriteLine("Hello World");
Console.WriteLine(10);
Console.WriteLine("Printing Integer: " + 10);
Console.WriteLine("Printing Any Number: " + 20.5);
Console.WriteLine("More Printing " + 20.5 + ", " + "50.2");
More Info:
// ------------------------------------
// Declaration & Initialization
// ------------------------------------
String variableName = "Some string";
String variableName = new String("Some string");
String variableName = """
Some string
with multiple lines
"""; // Text Blocks (Since Java 15)
// Const Declaration
final String MY_CONSTANT = "Some string";
// ------------------------------------
// Direct Access (Array-Like)
// ------------------------------------
char character = variableName.charAt(0);
// ------------------------------------
// Size / Length
// ------------------------------------
int length = variableName.length();
// ------------------------------------
// Trim
// ------------------------------------
String trimmed = variableName.trim();
String stripped = variableName.strip(); // Since Java 11, Unicode-aware
// ------------------------------------
// Is Null Or Empty
// ------------------------------------
boolean isNullOrEmpty = variableName == null || variableName.isEmpty();
boolean isNullOrBlank = variableName == null || variableName.isBlank(); // Since Java 11
// ------------------------------------
// Transform
// ------------------------------------
String upper = variableName.toUpperCase();
String lower = variableName.toLowerCase();
// ------------------------------------
// Compare
// ------------------------------------
boolean isEqual = variableName.equals(otherString);
boolean isEqualIgnoreCase = variableName.equalsIgnoreCase(otherString);
int result = variableName.compareTo(otherString);
// ------------------------------------
// Copy / Clone
// ------------------------------------
// Strings are immutable.
String copy = variableName;
String copy = new String(variableName);
// ------------------------------------
// Concatenation
// ------------------------------------
String result = firstString + secondString;
String result = firstString.concat(secondString);
String result = String.join(", ", values);
StringBuilder builder = new StringBuilder();
builder.append(firstString);
builder.append(secondString);
String result = builder.toString();
// ------------------------------------
// String Interpolation
// ------------------------------------
// No Native Support.
// Use String.format(), formatted() (Since Java 15), or concatenation.
String result = String.format("Hello %s", name);
String result = "Hello %s".formatted(name);
// ------------------------------------
// Print / Output (Old Format)
// ------------------------------------
// Main structure omitted
System.out.print("Hello world\n"); // '\n' is the new line character
System.out.println("Hello world");
System.out.println("Printing Integer: " + 10);
System.out.println("Printing Any Number: " + 20.5);
System.out.println("More Printing " + 20.5 + ", " + "50.2");
// ------------------------------------
// Declaration & Initialization
// ------------------------------------
let variable_name = "Some string"; // &str
let variable_name = String::from("Some string");
let variable_name = "Some string".to_string();
// Const Declaration
const MY_CONSTANT: &str = "Some string";
// ------------------------------------
// Direct Access (Array-Like)
// ------------------------------------
// Direct indexing is not available for String because UTF-8 characters are variable width.
let first_byte = variable_name.as_bytes()[0];
let first_character = variable_name.chars().next();
// ------------------------------------
// Size / Length
// ------------------------------------
let byte_length = variable_name.len();
let character_count = variable_name.chars().count();
// ------------------------------------
// Trim
// ------------------------------------
let trimmed = variable_name.trim();
let trimmed_start = variable_name.trim_start();
let trimmed_end = variable_name.trim_end();
// ------------------------------------
// Is Null Or Empty
// ------------------------------------
// Rust strings cannot be null.
let is_empty = variable_name.is_empty();
// ------------------------------------
// Transform
// ------------------------------------
let upper = variable_name.to_uppercase();
let lower = variable_name.to_lowercase();
// ------------------------------------
// Compare
// ------------------------------------
let is_equal = variable_name == other_string;
let result = variable_name.cmp(&other_string);
// ------------------------------------
// Copy / Clone
// ------------------------------------
let copy = variable_name.clone();
let copy = variable_name.to_string();
// ------------------------------------
// Concatenation
// ------------------------------------
let result = first_string + &second_string;
let result = format!("{}{}", first_string, second_string);
let mut result = String::new();
result.push_str(first_string);
result.push_str(second_string);
// ------------------------------------
// String Interpolation
// ------------------------------------
let result = format!("Hello {}", name);
let result = format!("{name}");
// ------------------------------------
// Print / Output (Old Format)
// ------------------------------------
println!("Hello World");
println!("Printing Integer: {}", 10);
println!("Printing Any Number: {}", 20.5);
println!("More Printing {}, {}", 20.5, 50.2);
// ------------------------------------
// Declaration & Initialization
// ------------------------------------
var variableName string = "Some string"
variableName := "Some string"
variableName := `Some string
with multiple lines`
// Const Declaration
const MY_CONSTANT string = "Some string"
// ------------------------------------
// Direct Access (Array-Like)
// ------------------------------------
// Indexing a string returns a byte.
firstByte := variableName[0]
// Use range or []rune for Unicode code points.
firstRune := []rune(variableName)[0]
// ------------------------------------
// Size / Length
// ------------------------------------
byteLength := len(variableName)
runeCount := utf8.RuneCountInString(variableName)
// ------------------------------------
// Trim
// ------------------------------------
trimmed := strings.TrimSpace(variableName)
trimmed := strings.Trim(variableName, " ")
trimmedStart := strings.TrimLeft(variableName, " ")
trimmedEnd := strings.TrimRight(variableName, " ")
// ------------------------------------
// Is Null Or Empty
// ------------------------------------
// Strings cannot be nil.
isEmpty := variableName == ""
// ------------------------------------
// Transform
// ------------------------------------
upper := strings.ToUpper(variableName)
lower := strings.ToLower(variableName)
// ------------------------------------
// Compare
// ------------------------------------
isEqual := variableName == otherString
result := strings.Compare(variableName, otherString)
// ------------------------------------
// Copy / Clone
// ------------------------------------
// Strings are immutable.
copyString := variableName
// ------------------------------------
// Concatenation
// ------------------------------------
result := firstString + secondString
result := strings.Join(values, ", ")
result := fmt.Sprintf("%s%s", firstString, secondString)
// ------------------------------------
// String Interpolation
// ------------------------------------
result := fmt.Sprintf("Hello %s", name)
// ------------------------------------
// Print / Output (Old Format)
// ------------------------------------
fmt.Println("Hello World")
fmt.Printf("Printing Integer: %d\n", 10)
fmt.Printf("Printing Any Number: %f\n", 20.5)
fmt.Printf("More Printing %f, %f\n", 20.5, 50.2)
// ------------------------------------
// Declaration & Initialization
// ------------------------------------
let variableName = 'Some string';
let variableName = "Some string";
var variableName = "Some string"; // Avoid 'var'
// Const Declaration
const variableName = "Some string";
const MY_CONSTANT = "Some string";
// ------------------------------------
// Direct Access (Array-Like)
// ------------------------------------
let character = variableName[0];
let character = variableName.charAt(0);
// ------------------------------------
// Size / Length
// ------------------------------------
let length = variableName.length;
// ------------------------------------
// Trim
// ------------------------------------
let trimmed = variableName.trim();
let trimmedStart = variableName.trimStart();
let trimmedEnd = variableName.trimEnd();
// ------------------------------------
// Is Null Or Empty
// ------------------------------------
let isNullOrEmpty = variableName == null || variableName.length === 0;
// ------------------------------------
// Transform
// ------------------------------------
let upper = variableName.toUpperCase();
let lower = variableName.toLowerCase();
// ------------------------------------
// Compare
// ------------------------------------
let isEqual = variableName === otherString;
let result = variableName.localeCompare(otherString);
// ------------------------------------
// Copy / Clone
// ------------------------------------
// Strings are immutable.
let copy = variableName;
// ------------------------------------
// Concatenation
// ------------------------------------
let result = firstString + secondString;
let result = `${firstString}${secondString}`;
let result = values.join(", ");
// ------------------------------------
// String Interpolation (Prefer)
// ------------------------------------
// Also known as Template literals (ES specification) or Template strings (old name)
console.log(`string text`);
console.log(`string text line 1
string text line 2`);
console.log(`string text ${expression} string text`);
console.log(`string text ${variable_name} string text ${variable_name} string text`);
const a = 5;
const b = 10;
console.log(`Fifteen is ${a + b} and not ${2 * a + b}.`);
// ------------------------------------
// Print / Output (Old Format)
// ------------------------------------
console.log('Hello World\n'); // '\n' is the new line character
console.log("Hello World\n");
console.log(10);
console.log("Printing Integer: " + 10);
console.log("Printing Integer: ", 10);
console.log("Printing Integer: %d\n", 10);
console.log("Printing Float: %f\n", 20.5);
console.log("Printing Any Number: " + 20.5);
console.log("Printing Any Number: ", 20.5);
console.log("More Printing " + 20.5 + ", " + "50.2\n");
console.log("More Printing ", 20.5, ", ", "50.2\n");
console.log([1, 2, 3, 4]); // array
console.log({a:1, b:2, c:3}); // object
console.log("Printing a Object: %o\n", {a:1, b:2, c:3});
More Info:
// ------------------------------------
// Declaration & Initialization
// ------------------------------------
let variableName: string = 'Some string';
// Const Declaration
const variableName: string = "Some string";
const MY_CONSTANT: string = "Some string";
// ------------------------------------
// Direct Access (Array-Like)
// ------------------------------------
let character: string = variableName[0];
let character: string = variableName.charAt(0);
// ------------------------------------
// Size / Length
// ------------------------------------
let length: number = variableName.length;
// ------------------------------------
// Trim
// ------------------------------------
let trimmed: string = variableName.trim();
let trimmedStart: string = variableName.trimStart();
let trimmedEnd: string = variableName.trimEnd();
// ------------------------------------
// Is Null Or Empty
// ------------------------------------
let isNullOrEmpty: boolean = variableName == null || variableName.length === 0;
// ------------------------------------
// Transform
// ------------------------------------
let upper: string = variableName.toUpperCase();
let lower: string = variableName.toLowerCase();
// ------------------------------------
// Compare
// ------------------------------------
let isEqual: boolean = variableName === otherString;
let result: number = variableName.localeCompare(otherString);
// ------------------------------------
// Copy / Clone
// ------------------------------------
// Strings are immutable.
let copy: string = variableName;
// ------------------------------------
// Concatenation
// ------------------------------------
let result: string = firstString + secondString;
let result: string = `${firstString}${secondString}`;
let result: string = values.join(", ");
// ------------------------------------
// String Interpolation (Prefer)
// ------------------------------------
// Also known as Template literals (ES specification) or Template strings (old name)
console.log(`string text`);
console.log(`string text line 1
string text line 2`);
console.log(`string text ${expression} string text`);
console.log(`string text ${variable_name} string text ${variable_name} string text`);
const a: number = 5;
const b: number = 10;
console.log(`Fifteen is ${a + b} and not ${2 * a + b}.`);
// ------------------------------------
// Print / Output (Old Format)
// ------------------------------------
console.log('Hello World\n'); // '\n' is the new line character
console.log("Hello World\n");
console.log(10);
console.log("Printing Integer: " + 10);
console.log("Printing Integer: ", 10);
console.log("Printing Integer: %d\n", 10);
console.log("Printing Float: %f\n", 20.5);
console.log("Printing Any Number: " + 20.5);
console.log("Printing Any Number: ", 20.5);
console.log("More Printing " + 20.5 + ", " + "50.2\n");
console.log("More Printing ", 20.5, ", ", "50.2\n");
console.log([1, 2, 3, 4]); // array
console.log({a:1, b:2, c:3}); // object
console.log("Printing a Object: %o\n", {a:1, b:2, c:3});
More Info:
# ------------------------------------
# Declaration & Initialization
# ------------------------------------
variable_name = "Some string"
variable_name: str = 'Some string' # Type Annotations (PEP 484 / Python 3.6+)
variable_name: any = 'Some string' # Type Annotations (PEP 484 / Python 3.6+)
# Const Declaration
MY_CONSTANT = 'Some string'
MY_CONSTANT: str = 'Some string' # Type Annotations (PEP 484 / Python 3.6+)
# In Python if you declare a variable in a block scope (if statement, for statement, ...)
# the variable is hoisted to the outer function scope.
# ------------------------------------
# Direct Access (Array-Like)
# ------------------------------------
character = variable_name[0]
# ------------------------------------
# Size / Length
# ------------------------------------
length = len(variable_name)
# ------------------------------------
# Trim
# ------------------------------------
trimmed = variable_name.strip()
trimmed_start = variable_name.lstrip()
trimmed_end = variable_name.rstrip()
# ------------------------------------
# Is Null Or Empty
# ------------------------------------
is_none_or_empty = variable_name is None or len(variable_name) == 0
# ------------------------------------
# Transform
# ------------------------------------
upper = variable_name.upper()
lower = variable_name.lower()
title = variable_name.title()
# ------------------------------------
# Compare
# ------------------------------------
is_equal = variable_name == other_string
is_not_equal = variable_name != other_string
# ------------------------------------
# Copy / Clone
# ------------------------------------
# Strings are immutable.
copy = variable_name
# ------------------------------------
# Concatenation
# ------------------------------------
result = first_string + second_string
result = "".join(values)
result = f"{first_string}{second_string}"
# ------------------------------------
# String Interpolation (3.6+) (Prefer)
# ------------------------------------
# Also known as f-Strings (3.6+)
# Example 1
name = "Eric"
age = 74
print(f"Hello, {name}. You are {age}.")
# Example 2
value = 10
print(f"{value=}")
print(f"{value:.2f}")
print(f"{value:08d}")
# ------------------------------------
# Print / Output (Old Format)
# ------------------------------------
print('Hello World')
print("Hello World\n", end='') # By default the print has '\n' appended after the last value, you can change it if you want.
print(10)
print("Printing Integer:", 10) # the ',' adds a space between the text and the value
print("Printing Float:", 20.5)
print("Printing Any Number:", 20.5)
print("More Printing", 20.5, ",", "50.2")
print([1, 2, 3, 4]) # array
print({"a":1, "b":2, "c":3}); # object
print({'a':1, 'b':2, 'c':3}); # object
print("Printing a Object: ", {"a":1, "b":2, "c":3})