Remove unknown characters at the end


#1

Is there a way to remove unknown characters from the end of a string?
eg. If I have 4 rows
23
1145a
48
1156m
How to remove the characters at the end?
Expected output:

23
1145
48
1156


#2

`Hi, Please try below

strinput= “123a”

bool isNumber = int32.TryParse(strInput,intVariable)

Ans: False

Then
strinput = strinput.Substring(0, (strinput.Length - 1))


#3

Hi,

Assuming that you need only numeric values :

Assign str = New String(str.Where(Function(c as Char) Char.IsNumber(c)).ToArray)

you can also use it in a for each row

For each row in dt
Assign Row(0) = New String(Row(0).toString.Where(Function(c as Char) Char.IsNumber(c)).ToArray)

Explanation :

You can create a new string using an array of char.

A string is a sequence of char, from this sequence we will take only the ones which are numeric and convert the sequence (of remaining char) to an array.

we will use this array to create the new string.

Cheers


#4

Alternatively,
You can remove all alphas and specials by using LINQ or Regex

String.Concat(text.Where(AddressOf Char.IsDigit))
or
System.Text.RegularExpressions.Regex.Replace(text, "[^0-9]", "")

“text” is variable with string
Those work well.


#6

Thanks a lot Clayton! Works like a charm :slightly_smiling_face:


#7

Thanks everyone for your help!


#8

Hi Clayton,

Iam having similar issue.Iam reading text by using get text .But Iam getting the result like \n\n\n\nCM1234\n\n. But I need this to be like CM as one variable and 1234 as other variable.

Could you please help me.


#9

Hi.
There are probably a few ways to do it.
Here’s one solution using Regex:

varAlphas = system.Text.RegularExpressions.Regex.Match(text.Trim,"[A-Z]{2,8}").Value
varNumbers = system.Text.RegularExpressions.Regex.Match(text.Trim,"[0-9]{2,8}").Value

Something like that would work, where it pulls the letters from 2 to 8 characters and pulls the digits from 2 to 8 characters

For additional help on Regex patterns to make it more sophisticated, do a search on Google.

Regards.


#10

Thanks Clayton.

One more need from you please.

My ouput was \n\nname:Clayton\nGroup:cse\n\n\n.But here Need the output should be Clayton and he it is sometimes clayton or any other.Always output will be between name: and group.

Please let me know


#11

If \n means a newline then I believe this pattern works:
“(?<=(((Name)|(name)|(NAME)):))[A-Za-z]{0,}”

system.Text.RegularExpressions.Regex.Match("\n\nname:Clayton\nGroup:cse\n\n\n","(?<=(((Name)|(name)|(NAME)):))[A-Za-z]{0,}").Value

the “?<=” is a positive look behind so basically it looks for the string that’s behind “Name” but does not include it in the value. Then, looks for alphas of any length.

You can probably do the same for Group too.

I hope this helps.

Regards.