• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Finding the length of a string

How can I find the length of a string and access separately each position:

say: $string = "ABCD"

and I need to use $access[1] = "B";
0
gservice
Asked:
gservice
  • 2
1 Solution
 
mattropeCommented:
To find length, use:
   $len = length($string);

To get/set a specific character, use:
   $char = substr($string, $offset, 1);
   substr($string,$offset,1) = "B";
0
 
mattropeCommented:
(By the way, the offset parameter to substr starts at 0 for the first character)
0
 
ozoCommented:
#if you want to deal with an array of characters, you can use
@access=split//,$string;
#but there are often better ways in perl to do things that you might do by accessing separately each position of a string in other languages.
#why do you need to use $access[1] = "B"; ?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now