Remove the last digits from a string

Hello,
I want to remove the last digits from a string without regex
input : hello4hello 1235 ( it’s dynamic maybe 2 digit or 3 …)
output : hello4hello

Hi @fzf

Welcome to the community!!

You can remove the last few digits using a string split. Split from space in between. Use the below command

Assuming your string variable is var1

Var1.Split(" "c)(0).ToString

This will split the string “hello4hello 12345” by the space and.return the first potion which is “hello4hello”

Hope it helps!!

If this works for you, please make sure to mark the answer as the solution too :slight_smile:

1 Like

You can directly remove by using split function

Var = hello4hello 12345

Var.split(“ “c).first

It will return you : hello4hello

But if i have “hello word 37489” it will not work

@Rashmi
But if i have “hello word 37489” it will not work.

But if i have “hello word 37489” it will not work

@fzf

Using regular expressions will help

Try this in a assign activity
var output = Regex.Replace(input, @"[\d-]", string.Empty)

Let know whether it works

@Lahiru.Fernando
I don’t want to work with Regex

Why not? It is the easiest way right?

@Lahiru.Fernando
Yes i know but I want work with split or trim or remove

Cool…

Try this.
Var = hello world 12345

Lets split this first to an array
Array1 = Var.Split(" "c)

Now array1 variable has separate elements of the string. And the last element contains the number you want to split

Lets remove the numbers in Var variable using the last element of the array

Var.Replace(array1(array1.length).ToString, "").Trim

This will work for you :slight_smile:

@Lahiru.Fernando

Input: Hello 4hello 5432
Output: Hello 4hello

I want this output

@fzf,

Try below one:

Str = “Hello 4hello 5432”

Str.Remove(Str.LastIndexOf(" "))

Hi,

Try:

text.TrimEnd(charsToTrim)

with text as your input, and charsToTrim an array of Chars

for example:
text: "hello4hello 1235"
charsToTrim: {"0"C, "1"C, "2"C, "3"C, "4"C, "5"C, "6"C, "7"C, "8"C, "9"C, " "C}

@lakshman
If i have this expression : “hello4 word 32 432” it wil not work

@msan the expression is dynamic
if i have for example “Hello word 32 754” or “Hello word 754” the space between hello and word will be remove

Really? I obtain "Hello word" in this case.

@fzf

Could you please show me all types of string expressions and then we will help you better.

I would recommend you to use regular expression to remove numbers from given string. It will give you better result.

@lakshman
Hello word 6 347
Helloword 1 543
Hello word 654
Helloword 654
Hello word 65
Helloword 65
Hello word 6
Helloword 6
Hello word
Helloword
and the output : Hello word or Helloword

@fzf

Try below one and will give you required output.

System.Text.RegularExpressions.Regex.Replace(yourString,"[\d-]",String.empty)