# String access in FreeFem++

**URL:** https://community.freefem.org/t/string-access-in-freefem/2498
**Category:** General Discussion
**Created:** [May 26, 2023, 7:48pm UTC](https://community.freefem.org/t/string-access-in-freefem/2498 "2023-05-26T19:48:28Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![galfras](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@galfras](https://community.freefem.org/u/galfras)
#### Post date: [May 26, 2023, 7:48pm UTC](https://community.freefem.org/t/string-access-in-freefem/2498/1 "2023-05-26T19:48:28Z")

</div>

Hello!  
Is it possible to access strings in FreeFem++? I am trying to replace specific letters in a string and I don’t know how to do it.

This is the idea of the code (fn is the string):

```auto
for(int i = 0; i<fn.length; i++){
		if(fn[i] == '/'){
		fn[i] = '\\';
	}

```

Thanks!

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [May 26, 2023, 8:00pm UTC](https://community.freefem.org/t/string-access-in-freefem/2498/2 "2023-05-26T20:00:19Z")

</div>

Yes, use the `fn.find("/")` to find the location of the first slash string as shown in [this](https://doc.freefem.org/examples/developers.html#string) example.

---

<div class="post-metadata">

### Author: ![galfras](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@galfras](https://community.freefem.org/u/galfras)
#### Post date: [May 26, 2023, 8:20pm UTC](https://community.freefem.org/t/string-access-in-freefem/2498/5 "2023-05-26T20:20:12Z")

</div>

Just in case anyone is struggling with the same, this is the way to access the string:

```auto
for(int i = 0; i<fn.length; i++){
		if(fn(i:i) == "/"){
		fn(i:i) = "\\";
                }
}

```

The problem I have now is with the “\”, since I get the error “End of string could not be found”

---

<div class="post-metadata">

### Author: ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)
#### Post date: [May 27, 2023, 8:11am UTC](https://community.freefem.org/t/string-access-in-freefem/2498/6 "2023-05-27T08:11:26Z")

</div>

I guess for anything more complicated, You could use exec. While slow it  
will be versatile. For example see below. This uses grep but you could also use sed  
or whatever. I left in the erroneous line because it took forever to find the problem lol.

```auto
exec("ls| grep edp > foo.txt");
exec("echo >> foo.txt");
ifstream ifs("foo.txt",binary);
string xxx;
//while (!ifs.eof) { getline(ifs, xxx) ; cout<<" line "<<xxx<endl; cout.flush; }
while (ifs.good()) { getline(ifs, xxx) ; cout<<" line "<<xxx<<endl; cout.flush; }

```

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [May 27, 2023, 11:22am UTC](https://community.freefem.org/t/string-access-in-freefem/2498/7 "2023-05-27T11:22:29Z")

</div>

Another solution:

```auto
while (fn.find("/") >= 0) fn(fn.find("/"):fn.find("/")) = "\\";

```
