
%   demonstration of string functions
%   IAM 550  Fall 2019
%   J. Raeder j.raeder@unh.edu

disp("Create a simple string:");
str="hello world"
pause

disp("A string array:");
stra = ["Mercury", "Gemini", "Apollo"]
pause

disp("concatenate strings")
s1 = "Good"
s2 = "morning"
s = strcat(s1,s2)
s=strcat(s1," ",s2)
pause

disp("format a number into a string:");
a=555.234,  str=string(a)
pause

%....... some string functions:
disp("string function erase:");
s="This is a string!"

t=erase(s,"!")
u=erase(s,"This")
pause

disp("split a string:");
s="This is a string"
x=split(s)
pause

disp("split at a specific delimiter:");
a="a:123:third:4th"
b=split(a,":")  %...... b is an array now!
pause

disp("the inverse operator: join")
a=["lion", "dog", "cat", "mouse"]
b=join(a,",")
b=join(a," eats ")
pause

disp("the delimiters can also be an array")
del=[" eats ", " and ", " devours "]
b=join(a,del)
pause

%  character arrays -->  single quote delimiter
disp("create a single line:");
name = 'Alice';   
age = 12;
X = [name,' will be ',num2str(age),' this year.'];
disp(X)
pause

disp("The sprintf and fprintf functions:");
name = 'Alice';   
age = 12;
X = sprintf('%s will be %d this year.',name,age);
disp(X)

name = 'Joe';   
age = 22;
fprintf('%s will be %d this year.\n',name,age);
pause

%.... all on string formatting:  
%  https://www.mathworks.com/help/matlab/ref/fprintf.html

%--->  show formatting table

%.... a busy example:
a=123.456
b=1884772
c="string"
disp("fprintf('|%s| |%15s| |%-15s| \n',c,c) -->");
fprintf('|%s| |%15s| |%-15s| \n\n',c,c,c)
pause
disp("fprintf('|%d| octal:|%o|  hex:|%x|  |%12d|  |%-12d| |%012d|\n', ");
fprintf('|%d| octal:|%o|  hex:|%x|  |%12d|  |%-12d| |%012d|\n\n', ...
    b,b,b,b,b,b)
pause
disp("fprintf('|%f| |%e| |%10.2f| |%10.2e| |%g| |%22.12f|\n',a,a,a,a,a,a)");
fprintf('|%f| |%e| |%10.2f| |%10.2e| |%g| |%22.12f|\n\n',a,a,a,a,a,a)

pause
disp("character arrays --> single quotes")
seq = 'GCTAGAATCC'
seq(4:6)



