How to delete only specific character and the end of a string

Hi,

I have problem that sometimes the user add unnecessary character at the end of a string.

For example,

  1. 000111|000222|000333|

  2. 000734|000143|012392|643221|213331|

It should be like this,

  1. 000111|000222|000333

  2. 000734|000143|012392|643221|213331

The “|” symbol only should be there when user wants to separate those 6 digits. It should not be at the end because after the “|” symbol, there is no 6 digit value.

Please show me how to tackle this problem. Thank you in advance

@Hakim_Asyraf - Please try this

System.text.RegularExpressions.Regex.Replace("000734|000143|012392|643221|213331|","\|$",string.Empty)

System.text.RegularExpressions.Regex.Replace(YourString","\|$",string.Empty)

Output

Hope this helps…

1 Like

Hi @Hakim_Asyraf

Please try this strText is your string

StrText = string.join(“|”,Split(strText,“|”).AsEnumerable().Where(function(d) d.ToString <> “”))

image

image

image

image

Thanks

1 Like

Hello @Hakim_Asyraf
the simplest solution you can go with is Trim like below:

string InputString ="000111|000222|000333|"

InputString = InputString.Trim("|"c)

You can find more info at: String.Trim Method (System) | Microsoft Learn

update: corrected code from Trim("|") to Trim("|"c) @prasath17 thank you :slight_smile:

2 Likes

Hi @Hakim_Asyraf ,

Try this also: stringRemove.xaml (4.5 KB)

strInput.Trim("|"c).ToString

or

If strInput.LastIndexOf(“|”) = (strInput.Length-1)

Then

strInput.Remove(strInput.Length-1).ToString

Thanks!

1 Like

Thankyou, this works for me

@AkshaySandhu - Awesome…But with one correction…Correct code is

image

This is missing the character

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.