xxxxxxxxxx
const string sText = " > Hallo vom C#-Buch V2.0! ";
Console.WriteLine(sText);
Console.WriteLine(sText.Length);
Console.WriteLine();
// Konvertierungs-Funktionen
Console.WriteLine(sText.ToLower());
Console.WriteLine(sText.ToUpper());
Console.WriteLine(sText.Trim());
Console.WriteLine();
// Trennungs-Funktionen
Console.WriteLine(sText.Split(' ').Length); // Alternative: Console.WriteLine(sText.Split(new char[] { ' ' }).Length);
Console.WriteLine(sText.Split(new string[] { " ", "-" }, StringSplitOptions.RemoveEmptyEntries).Length);
Console.WriteLine(sText.Substring(4, 5));
Console.WriteLine();
// Such-Funktionen
Console.WriteLine(sText.IndexOf(' '));
Console.WriteLine(sText.LastIndexOf(' '));
Console.WriteLine();
// Prüf-Funktionen (gibt true oder false zurück)
Console.WriteLine(sText.Contains("hallo")); // wird nicht gefunden (mit "Hallo" wäre es gefunden worden)
Console.WriteLine(sText.StartsWith("C"));
Console.WriteLine(sText.EndsWith("!"));
Console.WriteLine();
// Veränderungs-Funktionen
Console.WriteLine(sText.Insert(24, "1")); // aus V2.0 wird V2.10
Console.WriteLine(sText.Remove(9, 4)); // entfernt "vom"
Console.ReadKey();
xxxxxxxxxx
#include <iostream>
int main()
{
using namespace std;
char name[20]; //declaring string 'name'
cin.getline(name, sizeof(name)); //taking string input
cout << name << endl; //printing string
return 0;
}
xxxxxxxxxx
public class Test {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
s2 = s2.intern();
System.out.println(s1 == s2);
}
}
xxxxxxxxxx
/*
*NOTICE: the 2 variables refer to one place in memory no 2 different,
because both of them have the same value.
*/
String str1 = "Hello";
String str2 = "Hello";
/* if I changed value of each one will not share the common place. */
String str2 = "HELLO";
/* Now, both of them has different place in memory */
xxxxxxxxxx
[0]
In computer programming, a string is traditionally a sequence of characters,
either as a literal constant or as some kind of variable. The latter may
allow its elements to be mutated and the length changed, or it may be fixed
(after creation). A string is generally considered as a data type and is
often implemented as an array data structure of bytes (or words) that stores
a sequence of elements, typically characters, using some character encoding.
String may also denote more general arrays or other sequence (or list) data
types and structures.
Depending on the programming language and precise data type used, a variable
declared to be a string may either cause storage in memory to be statically
allocated for a predetermined maximum length or employ dynamic allocation to
allow it to hold a variable number of elements.
When a string appears literally in source code, it is known as a string
literal or an anonymous string.
In formal languages, which are used in mathematical logic and theoretical
computer science, a string is a finite sequence of symbols that are chosen
from a set called an alphabet.
[1]
The string type is used to store a sequence of characters (text). This is not
a built-in type, but it behaves like one in its most basic usage. String
values must be surrounded by double quotes
xxxxxxxxxx
var string = "Alura";
var resultado = string.substring(1, 4);COPIAR CÓDIGO
xxxxxxxxxx
var greeting = 'Hi buddy';
var message = 'You are doing great! Keep studying!';
xxxxxxxxxx
class Athlete {
public String athleteName;
public double athleteSpeed;
public int athleteAge;
}
The next basic data type is the string. Strings are used to represent text. They are written by enclosing their content in quotes.
xxxxxxxxxx
`Down on the sea`
"Lie on the ocean"
'Float on the ocean'
You can use single quotes, double quotes, or backticks to mark strings, as long as the quotes at the start and the end of the string match.
A group of characters such as this is an example of the string data type.
A string is a collection of characters closed within single, double or triple quotation marks.
A string can also contain a single character or be entirely empty.
xxxxxxxxxx
print("Harry Potter!") # Double quotation marks
got = 'Game of Thrones...' # Single quotation marks
print(got)
print("$") # Single character
empty = ""
print(empty) # Just prints an empty line
multiple_lines = '''Triple quotes allows
multi-line string.'''
print(multiple_lines)