Sign in to follow this  
Followers 0
Seminko

Removing a number from a string

16 posts in this topic

Hey fellas,

I have these text strings in an array:

Customer Details 1
Site Details 2
Slough Primary 2
Slough Secondary 5
Dynamic Bandwidth 9
Billing Details 14

How do I remove the last space and number so I have this:

Customer Details
Site Details
Slough Primary
Slough Secondary
Dynamic Bandwidth
Billing Details

Please note the in this example we have only two words in the strings but it can easily be more ie: "Slough UK based Primary" etc. Also, the numbers can be either one, two or even three digits long.

Thank you!

Share this post


Link to post
Share on other sites



Seminko,

This seems to do the trick:

#include <Array.au3>

Global $aArray[] = ["Customer Details 1", "Site Details 2", "Slough Primary 2", "Slough Secondary 5", "Dynamic Bandwidth 9", "Billing Details 14"]

For $i = 0 To UBound($aArray) - 1
    $aArray[$i] = StringRegExpReplace($aArray[$i], "\s(\d+)", "")
Next

_ArrayDisplay($aArray, "", Default, 8)
Basically remove anything that matches "space followed by at least 1 digit until the next non-digit". ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Another take on it, allows numbers in the text...

#include <array.au3>

Local $aBefore = [ _
        'Customer9 Details3 1', _
        'Site 14 Details 2', _
        'Slough Primary 2', _
        'Slough Secondary 5', _
        '14Dynamic Bandwidth 9', _
        'Billing Details 14' _
        ]

For $1 = 0 To UBound($aBefore) - 1
    $aBefore[$1] = StringRegExpReplace($aBefore[$1], '([\w+]) \d+$', '\1')
Next

_ArrayDisplay($aBefore)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hey Melba, thanks!

Well that got me thinking because there MIGHT be a case where there is a number in the title, say something like: "Customer 1 Details 9" and I would like for it to return: "Customer 1 Details"

It might help that before I StringSplit the string into an array there were line breaks (I used @CRLF as a delimiter) just like you see in my example. So let's say this is how the string returned by ClipGet looks like:

Customer Details 1
Site Details 2
Slough Primary 2
Slough Secondary 5
Dynamic Bandwidth 9
Billing Details 14

Could we do sth like: look for a space followed by at least 1 digit until line break (@CRLF)?

Thanks

Edited by Seminko

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

kylomas,

Well, if you want to be fussy: :P

#include <Array.au3>

Global $aArray[] = ["Customer Details 1", "Site Details 2", "Slough 14 Primary 2", "Slough Secondary 5", "Dynamic Bandwidth 9", "Billing Details 14"]

For $i = 0 To UBound($aArray) - 1
    $aArray[$i] = StringRegExpReplace($aArray[$i], "^(.*)\s\d+$", "$1")
Next

_ArrayDisplay($aArray, "", Default, 8)
M23

Edit:

Seminko,

Looks like we were there ahead of you! :D

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Yes, any other changes?


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

I have got to find a smiley of a guy pissing out of an airplane... :rolleyes:


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Another take on it, allows numbers in the text...

#include <array.au3>

Local $aBefore = [ _
        'Customer9 Details3 1', _
        'Site 14 Details 2', _
        'Slough Primary 2', _
        'Slough Secondary 5', _
        '14Dynamic Bandwidth 9', _
        'Billing Details 14' _
        ]

For $1 = 0 To UBound($aBefore) - 1
    $aBefore[$1] = StringRegExpReplace($aBefore[$1], '([\w+]) \d+$', '\1')
Next

_ArrayDisplay($aBefore)

kylomas

 

Exactly what I needed. Thanks both of you guys for such a swift response! I can move on now :)

Share this post


Link to post
Share on other sites

Use this one.  It allows for any number of spaces between the text you want and the numbers...

#include <array.au3>

Local $aBefore = [ _
        'Customer9 Details3 1', _
        'Site 14 Details 2', _
        'Slough Primary 2', _
        'Slough Secondary   5', _
        '14Dynamic Bandwidth 9', _
        'Billing Details 14' _
        ]

For $1 = 0 To UBound($aBefore) - 1
    $aBefore[$1] = StringRegExpReplace($aBefore[$1], '([\w+])\s+\d+$', '\1')
Next

_ArrayDisplay($aBefore)

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

I have got to find a smiley of a guy pissing out of an airplane... :rolleyes:

Its not from an airplane, but this might work.

peeing-in-the-snow.gif

'>

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

@willichan Thanks but it does not have a M23 feel to it...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

kylomas,

I rather hope "a guy pissing out of an airplane" does not have "a M23 feel to it" either! :

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

@M23 - Of course not!!  Something more along the lines of the 'Red Baron'...


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

kylomas,

([w+])  seems to make no sense, as there is no "+" in the strings and this matches any word char in the string (except in the part to be deleted)

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

@mikell, yes, you're right,  thank you...

edit: I like jguinch's solution...

#include <array.au3>

Local $aBefore = [ _
        'Customer9 Details3 1', _
        'Site 14 Details 2', _
        'Slough Primary 2', _
        'Slough Secondary 12 5', _
        '14Dynamic Bandwidth 9', _
        'Billing Details 14' _
        ]

For $1 = 0 To UBound($aBefore) - 1
    $aBefore[$1] = StringRegExpReplace($aBefore[$1], '\s\d+$', '')
Next

_ArrayDisplay($aBefore)
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0