Does anyone know how to extract the nth character from a string- nice simple code like:

charfromstring(mystring, 33) to extract the 33rd character of mystring

obviously this is wrong- what is the correct way to do it.

Even better, how could I split a string at the nth character

eg.
1 My name is John

2 My name is Brian

3 My name is George

Split at 10th character:
1 My name is

a John

2 My name is

b Brian

3 My name is

c George

Hi!
Hope this detailed comment helps you!
First of all let me tell you that in delphi( and pascal ) the variable type string is a type that sometimes could be used directly like a variable type array.
It means in order to acess the nth char of a string, you write: MyString[n]
ie: MyString:='I AM A BLACKBOARD!' then MyString[4] would be 'M', the 4th char of MyString.

If you want to statically declare a string with a length of n, then use:
var MyString: String[n] which generates a string of length n.

note that the element 0 (MyString[0]) is indicator of its length and is inaccessible!
if you are using dynamic range strings (var MyString: String) then use
Length(MyString) to read length or
SetLength(MyString) to set it instead;

For the next part , now you are enabled for example to split the string from the nth char.
ie: If you have a string MyString1 of length 10 and MyString2 of length 5:
setlength(MyString1, 10)
setlength(MyString2, 5)
MyString2:= MyString1
Because MyString2 has not enough capacity for all chars in MyString1, then MyString2 would be consist of MyString1 upto the 5th char;

Wish you success! And good luck.

Thanks so much! it works really well.
var
strng:string;
charno:integer;


begin

Writeln('Input string');
Readln(strng);
charno:=1;
While strng[charno]<>' ' do
begin
Writeln(strng[charno]);
readln;
charno:=charno+1;
end;
Writeln('End of word!') ;
While strng[charno+2]<>' ' do
begin
charno:=charno+1;
While strng[charno]<>' ' do
begin
Writeln(strng[charno]);
readln;
charno:=charno+1;
end;
Writeln('End of word');
end;
Writeln('End of string');
Readln

end.

Hi!
Hope this detailed comment helps you!
First of all let me tell you that in delphi( and pascal ) the variable type string is a type that sometimes could be used directly like a variable type array.
It means in order to acess the nth char of a string, you write: MyString[n]
ie: MyString:='I AM A BLACKBOARD!' then MyString[4] would be 'M', the 4th char of MyString.

If you want to statically declare a string with a length of n, then use:
var MyString: String[n] which generates a string of length n.

note that the element 0 (MyString[0]) is indicator of its length and is inaccessible!
if you are using dynamic range strings (var MyString: String) then use
Length(MyString) to read length or
SetLength(MyString) to set it instead;

For the next part , now you are enabled for example to split the string from the nth char.
ie: If you have a string MyString1 of length 10 and MyString2 of length 5:
setlength(MyString1, 10)
setlength(MyString2, 5)
MyString2:= MyString1
Because MyString2 has not enough capacity for all chars in MyString1, then MyString2 would be consist of MyString1 upto the 5th char;

Wish you success! And good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.