Creating substrings in F#

Reading a chunk of a string into another string is based on the position of the characters in the string. The characters in the string form an array of chars and we can read parts of it by referring to the required array positions of the characters. This is no different from popular OOP languages like C# and Java. The only difference is the syntax.

Let’s start with the following string:

let longString = "The message couples the distressed heat before a purchase."

The random sentence generator site has a lot of similar meaningless sentences for testing.

The most simplistic application of substrings is to read one character:¨

let longString = "The message couples the distressed heat before a purchase."
let singleChar = longString.[5]

singleChar will be ‘e’ since it has position 5 in a 0 based array in the string: T-h-e-whitespace-m-e, that’s position 0, 1, 2, 3, 4 and 5 respectively. Note how the array element selector must be connected with the string using a dot.

The selector can accept a range like here:

let longString = "The message couples the distressed heat before a purchase."
let substring = longString.[20..]

This selects the string from position 20 to the end, i.e. “the distressed heat before a purchase.”.

Conversely we can only provide the last position:

let longString = "The message couples the distressed heat before a purchase."
let substring = longString.[..18]

…which gives “The message couples”.

Finally we can provide both the start and end position:

let substring = longString.[4..10]

…which will be equal to “message”.

View all F# related articles here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: