Code Snippet: Access Individual Characters

From Coder Merlin
Jump to navigation Jump to search

Swift[edit]

let s = "Alpha:Bravo:Charlie:Delta" 
let a = Array(s)                                                                                                                                                                                         
print("The fourth character is: \(a[3])")

Java[edit]

String s = "Alpha:Bravo:Charlie:Delta";
System.out.println("The fourth character is " + s.charAt(3));

Python[edit]

s = "Alpha:Bravo:Charlie:Delta" 
print("The fourth character is: " + s[3])

C++[edit]

#include <iostream>
#include <string>

int main() {
  const std::string s = "Alpha:Bravo:Charlie:Delta";
  std::cout << "The fourth character is " << s.at(3);
  return  0;
}


Designed with pride in Silicon Valley, CA, USA