{"id":3647,"date":"2024-05-28T06:11:21","date_gmt":"2024-05-28T06:11:21","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=3647"},"modified":"2024-05-28T06:11:22","modified_gmt":"2024-05-28T06:11:22","slug":"string","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/05\/28\/string\/","title":{"rendered":"String"},"content":{"rendered":"\n<p>The String data type represents a sequence of characters. A Dart string is a sequence of UTF 16 code units.<\/p>\n\n\n\n<p>String values in Dart can be represented using either single or double or triple quotes. Single line strings are represented using single or double quotes. Triple quotes are used to represent multi-line strings.<\/p>\n\n\n\n<p>The syntax of representing string values in Dart is as given below \u2212<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>String  variable_name = 'value'  \n\nOR  \n\nString  variable_name = ''value''  \n\nOR  \n\nString  variable_name = '''line1 \nline2'''  \n\nOR  \n\nString  variable_name= ''''''line1 \nline2''''''\n<\/code><\/pre>\n\n\n\n<p>The following example illustrates the use of String data type in Dart.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() { \n   String str1 = 'this is a single line string'; \n   String str2 = \"this is a single line string\"; \n   String str3 = '''this is a multiline line string'''; \n   String str4 = \"\"\"this is a multiline line string\"\"\"; \n   \n   print(str1);\n   print(str2); \n   print(str3); \n   print(str4); \n}<\/code><\/pre>\n\n\n\n<p>It will produce the following&nbsp;<strong>Output<\/strong>&nbsp;\u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this is a single line string \nthis is a single line string \nthis is a multiline line string \nthis is a multiline line string \n<\/code><\/pre>\n\n\n\n<p>Strings are immutable. However, strings can be subjected to various operations and the resultant string can be a stored as a new value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">String Interpolation<\/h2>\n\n\n\n<p>The process of creating a new string by appending a value to a static string is termed as&nbsp;<strong>concatenation<\/strong>&nbsp;or&nbsp;<strong>interpolation<\/strong>. In other words, it is the process of adding a string to another string.<\/p>\n\n\n\n<p>The operator plus (+) is a commonly used mechanism to concatenate \/ interpolate strings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() { \n   String str1 = \"hello\"; \n   String str2 = \"world\"; \n   String res = str1+str2; \n   \n   print(\"The concatenated string : ${res}\"); \n}<\/code><\/pre>\n\n\n\n<p>It will produce the following&nbsp;<strong>output<\/strong>&nbsp;\u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The concatenated string : Helloworld\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2<\/h3>\n\n\n\n<p>You can use &#8220;${}&#8221; can be used to interpolate the value of a Dart expression within strings. The following example illustrates the same.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() { \n   int n=1+1; \n   \n   String str1 = \"The sum of 1 and 1 is ${n}\"; \n   print(str1); \n   \n   String str2 = \"The sum of 2 and 2 is ${2+2}\"; \n   print(str2); \n}<\/code><\/pre>\n\n\n\n<p>It will produce the following&nbsp;<strong>output<\/strong>&nbsp;\u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The sum of 1 and 1 is 2 \nThe sum of 2 and 2 is 4\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">String Properties<\/h2>\n\n\n\n<p>The properties listed in the following table are all read-only.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Sr.No<\/th><th>Property &amp; Description<\/th><\/tr><tr><td>1<\/td><td>codeUnitsReturns an unmodifiable list of the UTF-16 code units of this string.<\/td><\/tr><tr><td>2<\/td><td>isEmptyReturns true if this string is empty.<\/td><\/tr><tr><td>3<\/td><td>LengthReturns the length of the string including space, tab and newline characters.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Methods to Manipulate Strings<\/h2>\n\n\n\n<p>The String class in the&nbsp;<strong>dart: core library<\/strong>&nbsp;also provides methods to manipulate strings. Some of these methods are given below \u2212<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Sr.No<\/th><th>Methods &amp; Description<\/th><\/tr><tr><td>1<\/td><td>toLowerCase()Converts all characters in this string to lower case.<\/td><\/tr><tr><td>2<\/td><td>toUpperCase()Converts all characters in this string to upper case.<\/td><\/tr><tr><td>3<\/td><td>trim()Returns the string without any leading and trailing whitespace.<\/td><\/tr><tr><td>4<\/td><td>compareTo()Compares this object to another.<\/td><\/tr><tr><td>5<\/td><td>replaceAll()Replaces all substrings that match the specified pattern with a given value.<\/td><\/tr><tr><td>6<\/td><td>split()Splits the string at matches of the specified delimiter and returns a list of substrings.<\/td><\/tr><tr><td>7<\/td><td>substring()Returns the substring of this string that extends from startIndex, inclusive, to endIndex, exclusive.<\/td><\/tr><tr><td>8<\/td><td>toString()Returns a string representation of this object.<\/td><\/tr><tr><td>9<\/td><td>codeUnitAt()Returns the 16-bit UTF-16 code unit at the given index.<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The String data type represents a sequence of characters. A Dart string is a sequence of UTF 16 code units. String values in Dart can be represented using either single or double or triple quotes. Single line strings are represented using single or double quotes. Triple quotes are used to represent multi-line strings. The syntax [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[251],"tags":[],"class_list":["post-3647","post","type-post","status-publish","format-standard","hentry","category-04-dart"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=3647"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3647\/revisions"}],"predecessor-version":[{"id":3648,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3647\/revisions\/3648"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=3647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=3647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=3647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}