Solved

extract the number from a string

Posted on 2014-04-28
2
584 Views
Last Modified: 2014-04-28
Hi,

if I had a variable which consisted of a string like this $a = "abc-wertr01" sometimes the lenght of the sting can be longer. How can I extract the last two characters from the string?

I want to increase the number in the string by 1.. and to do that I need to turn that part of the string into an integer

Thanks

$TempName = "abc-wertr01"
$a = $TempName
$b = $a.Length
$c = $b - 2
$d = $a.substring($c)
$e = [INT] $d
$f = "{0:D2}" -f $e
$e++

Open in new window

0
Comment
Question by:patelbg2001
2 Comments
 
LVL 6

Accepted Solution

by:
patelbg2001 earned 0 total points
ID: 40026976
worked it out
$TempName = "abc-wertr01"
$a = $TempName
$b = $a.substring($a.length -2, 2)

Open in new window

0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40026985
You need
$str.substring($str.length -2, 2)

where $str is your string.

LE: too slow :)

HTH,
Dan
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question