Jump to content

Removing a number from a string


Go to solution Solved by kylomas,

Recommended Posts

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!

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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 :)

Link to comment
Share on other sites

  • Solution

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

Link to comment
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

'>

Link to comment
Share on other sites

  • Moderators

kylomas,

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

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

@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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...