How to Declare Strings in various languages?
Below is the representation of strings in various languages:
C++
// C++ program to demonstrate String // using Standard String representation #include <iostream> #include <string> using namespace std; int main() { // Declare and initialize the string string str1 = "Welcome to w3wiki!" ; // Initialization by raw string string str2( "A Computer Science Portal" ); // Print string cout << str1 << endl << str2; return 0; } |
C
// C program to illustrate strings #include <stdio.h> int main() { // declare and initialize string char str[] = "Geeks" ; // print string printf ( "%s" , str); return 0; } |
Java
// Java code to illustrate String import java.io.*; import java.lang.*; class Test { public static void main(String[] args) { // Declare String without using new operator String s = "w3wiki" ; // Prints the String. System.out.println( "String s = " + s); // Declare String using new operator String s1 = new String( "w3wiki" ); // Prints the String. System.out.println( "String s1 = " + s1); } } |
Python
# Python Program for # Creation of String # Creating a String # with single Quotes String1 = 'Welcome to the Geeks World' print ( "String with the use of Single Quotes: " ) print (String1) # Creating a String # with double Quotes String1 = "I'm a Geek" print ( "\nString with the use of Double Quotes: " ) print (String1) # Creating a String # with triple Quotes String1 = '''I'm a Geek and I live in a world of "Geeks"''' print ( "\nString with the use of Triple Quotes: " ) print (String1) # Creating String with triple # Quotes allows multiple lines String1 = '''Geeks For Life''' print ( "\nCreating a multiline String: " ) print (String1) |
C#
// Include namespace system using System; public class Test { public static void Main(String[] args) { // Declare String without using new operator var s = "w3wiki" ; // Prints the String. Console.WriteLine( "String s = " + s); // Declare String using new operator var s1 = new String( "w3wiki" ); // Prints the String. Console.WriteLine( "String s1 = " + s1); } } |
Javascript
<!DOCTYPE html> <html> <head> <title> JavaScript Strings </title> </head> <body> <h1>w3wiki</h1> <h2>JavaScript Strings</h2> <p id= "GFG" ></p> <!-- Script to store string in variable --> <script> // String written inside quotes var x = "Welcome to w3wiki!" ; document.getElementById( "GFG" ).innerHTML = x; </script> </body> </html> |
PHP
<?php // single-quote strings $site = 'Welcome to w3wiki' ; echo $site ; ?> |
Contact Us