1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[2390],{22254(e,s,n){n.r(s),n.d(s,{assets:()=>d,contentTitle:()=>l,default:()=>h,frontMatter:()=>i,metadata:()=>r,toc:()=>c});const r=JSON.parse('{"id":"language/data_types/strings","title":"Strings","description":"Discover the String data type in Noir. Learn about its methods, see real-world examples, and understand how to effectively manipulate and use Strings in Noir.","source":"@site/processed-docs/language/data_types/strings.md","sourceDirName":"language/data_types","slug":"/language/data_types/strings","permalink":"/docs/dev/language/data_types/strings","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/docs/language/data_types/strings.md","tags":[],"version":"current","frontMatter":{"title":"Strings","description":"Discover the String data type in Noir. Learn about its methods, see real-world examples, and understand how to effectively manipulate and use Strings in Noir.","keywords":["noir","string type","methods","examples","concatenation"]},"sidebar":"sidebar","previous":{"title":"Booleans","permalink":"/docs/dev/language/data_types/booleans"},"next":{"title":"Arrays","permalink":"/docs/dev/language/data_types/arrays"}}');var t=n(74848),a=n(28453);const i={title:"Strings",description:"Discover the String data type in Noir. Learn about its methods, see real-world examples, and understand how to effectively manipulate and use Strings in Noir.",keywords:["noir","string type","methods","examples","concatenation"]},l=void 0,d={},c=[{value:"Escape characters",id:"escape-characters",level:2},{value:"Raw strings",id:"raw-strings",level:2},{value:"Format strings",id:"format-strings",level:2}];function o(e){const s={a:"a",code:"code",em:"em",h2:"h2",p:"p",pre:"pre",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",...(0,a.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(s.p,{children:["The string type is a fixed length value defined with ",(0,t.jsx)(s.code,{children:"str<N>"}),"."]}),"\n",(0,t.jsxs)(s.p,{children:["You can use strings in ",(0,t.jsx)(s.code,{children:"assert()"})," functions or print them with\n",(0,t.jsx)(s.code,{children:"println()"}),". See more about ",(0,t.jsx)(s.a,{href:"/docs/dev/libraries/standard_library/logging",children:"Logging"}),"."]}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-rust",children:'\nfn main(message : pub str<11>, hex_as_string : str<4>) {\n println(message);\n assert(message == "hello world");\n assert(hex_as_string == "0x41");\n}\n'})}),"\n",(0,t.jsxs)(s.p,{children:["You can convert a ",(0,t.jsx)(s.code,{children:"str<N>"})," to a byte array by calling ",(0,t.jsx)(s.code,{children:"as_bytes()"}),"\nor a vector by calling ",(0,t.jsx)(s.code,{children:"as_bytes_vec()"}),"."]}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-rust",children:'fn main() {\n let message = "hello world";\n let message_bytes = message.as_bytes();\n let mut message_vec = message.as_bytes_vec();\n assert(message_bytes.len() == 11);\n assert(message_bytes[0] == 104);\n assert(message_bytes[0] == message_vec.get(0));\n}\n'})}),"\n",(0,t.jsx)(s.h2,{id:"escape-characters",children:"Escape characters"}),"\n",(0,t.jsx)(s.p,{children:"You can use escape characters for your strings:"}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,t.jsxs)(s.table,{children:[(0,t.jsx)(s.thead,{children:(0,t.jsxs)(s.tr,{children:[(0,t.jsx)(s.th,{children:"Escape Sequence"}),(0,t.jsx)(s.th,{children:"Description"})]})}),(0,t.jsxs)(s.tbody,{children:[(0,t.jsxs)(s.tr,{children:[(0,t.jsx)(s.td,{children:(0,t.jsx)(s.code,{children:"\\r"})}),(0,t.jsx)(s.td,{children:"Carriage Return"})]}),(0,t.jsxs)(s.tr,{children:[(0,t.jsx)(s.td,{children:(0,t.jsx)(s.code,{children:"\\n"})}),(0,t.jsx)(s.td,{children:"Newline"})]}),(0,t.jsxs)(s.tr,{children:[(0,t.jsx)(s.td,{children:(0,t.jsx)(s.code,{children:"\\t"})}),(0,t.jsx)(s.td,{children:"Tab"})]}),(0,t.jsxs)(s.tr,{children:[(0,t.jsx)(s.td,{children:(0,t.jsx)(s.code,{children:"\\0"})}),(0,t.jsx)(s.td,{children:"Null Character"})]}),(0,t.jsxs)(s.tr,{children:[(0,t.jsx)(s.td,{children:(0,t.jsx)(s.code,{children:'\\"'})}),(0,t.jsx)(s.td,{children:"Double Quote"})]}),(0,t.jsxs)(s.tr,{children:[(0,t.jsx)(s.td,{children:(0,t.jsx)(s.code,{children:"\\\\"})}),(0,t.jsx)(s.td,{children:"Backslash"})]})]})]}),"\n",(0,t.jsx)(s.p,{children:"Example:"}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-rust",children:'let s = "Hello \\"world" // prints "Hello "world"\nlet s = "hey \\tyou"; // prints "hey you"\n'})}),"\n",(0,t.jsx)(s.h2,{id:"raw-strings",children:"Raw strings"}),"\n",(0,t.jsxs)(s.p,{children:["A raw string begins with the letter ",(0,t.jsx)(s.code,{children:"r"})," and is optionally delimited by a number of hashes ",(0,t.jsx)(s.code,{children:"#"}),"."]}),"\n",(0,t.jsxs)(s.p,{children:["Escape characters are ",(0,t.jsx)(s.em,{children:"not"})," processed within raw strings. All contents are interpreted literally."]}),"\n",(0,t.jsx)(s.p,{children:"Example:"}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-rust",children:'let s = r"Hello world";\nlet s = r#"Simon says "hello world""#;\n\n// Any number of hashes may be used (>= 1) as long as the string also terminates with the same number of hashes\nlet s = r#####"One "#, Two "##, Three "###, Four "####, Five will end the string."#####; \n'})}
1),"\n",(0,t.jsx)(s.h2,{id:"format-strings",children:"Format strings"}),"\n",(0,t.jsxs)(s.p,{children:["A format string begins with the letter ",(0,t.jsx)(s.code,{children:"f"})," and allows inserting the value of local and global variables in it."]}),"\n",(0,t.jsx)(s.p,{children:"Example:"}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-rust",children:'let four = 2 + 2;\nlet s = f"Two plus two is: {four}";\nprintln(s);\n'})}),"\n",(0,t.jsx)(s.p,{children:"The output of the above program is:"}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-text",children:"Two plus two is: 4\n"})}),"\n",(0,t.jsxs)(s.p,{children:["To insert the value of a local or global variable, put it inside ",(0,t.jsx)(s.code,{children:"{...}"})," in the string."]}),"\n",(0,t.jsxs)(s.p,{children:["If you need to write the ",(0,t.jsx)(s.code,{children:"{"})," or ",(0,t.jsx)(s.code,{children:"}"})," characters, use ",(0,t.jsx)(s.code,{children:"{{"})," and ",(0,t.jsx)(s.code,{children:"}}"})," respectively:"]}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-rust",children:'let four = 2 + 2;\n\n// Prints "This is not expanded: {four}"\nprintln(f"This is not expanded: {{four}}");\n'})}),"\n",(0,t.jsxs)(s.p,{children:["More complex expressions are not allowed inside ",(0,t.jsx)(s.code,{children:"{...}"}),":"]}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-rust",children:'let s = f"Two plus two is: {2 + 2}" // Error: invalid format string\n'})})]})}function h(e={}){const{wrapper:s}={...(0,a.R)(),...e.components};return s?(0,t.jsx)(s,{...e,children:(0,t.jsx)(o,{...e})}):o(e)}},28453(e,s,n){n.d(s,{R:()=>i,x:()=>l});var r=n(96540);const t={},a=r.createContext(t);function i(e){const s=r.useContext(a);return r.useMemo(function(){return"function"==typeof e?e(s):{...s,...e}},[s,e])}function l(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(t):e.components||t:i(e.components),r.createElement(a.Provider,{value:s},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.