How to manipulate a part of string: Split, Trim, Substring, Replace, Remove, Left, Right

Thank you for your efforts, its quite helpful.

1 Like

Hi @Adrian,

How can we remove a blank line after our text? I’ve tried with “Trim” but without success.

My text is:
Client ID: PU23971
Client Name: Chance Strittmatter
Client Country: Germany

And I want the string “PU23971-Chance Strittmatter-Germany”

I’m using those methods:
ClientID = Trim(Split((Split(ClientInformation,"Client ID: ")(1).ToString),"Client Name: ")(0)).ToString
ClientName= Trim(Split((Split(ClientInformation,"Client Name: ")(1).ToString),"Client Country: “)(0)).ToString
ClientCountry = Trim(ClientInformation.Split(”:"c)(3))

Type into: ClientID + “-” + ClientName + “-” + ClientCountry

But I’m having this:

image :sob:

1 Like

Hi @Airun, You can try:

textString.Replace(vbCr, "").Replace(vbLf, "")

Remove New Line from whole string:

Replace(vbCr, "")- removes \r [CR (Carriage Return)]
Replace(vbLf, "") - removes \n [LF (Line Feed, Environment.NewLine)]
Replace(vbCrLf, "") - removes \r\n [CR+LF ( End Of Line)]

Remove New Line only from the end of string:

textString.TrimEnd(vbCr.ToCharArray)
textString.TrimEnd(vbCrLf.ToCharArray)
textString.TrimEnd(vbCrLf.ToCharArray)

7 Likes

Perfect!!! Thank you very much!!! :star_struck: :partying_face:

image

2 Likes

Hello,
In this video I do a lot of stuff with String:

0:35 Examples for Substring functions
4:10 IndexOf and LastIndexOf
5:00 SubString working together IndexOf and LastIndexOf
6:45 Split string by character
8:50 Split string by string
12:00 Lower Case and Upper Case
12:45 Trim
15:05 Compare strings in multiple ways
19:05 Resume of all the String function part

Thanks,
Cristian Negulescu

6 Likes

thank you for the wonderful gifts

1 Like

how can I use substring in order to check keywords in the first 100 characters of email body. I tried to use it as - item.Body.Substring(0,100).Contains(“stop”)
however, I am getting an error for it.

HI, apparently your Body email is not always 100 characters long. You can first measure the length of a string with?:
item.Body.Lenght (as Int 32 variable), and then substitute a variable containing the length of characters under the limit substring.
if lenght >= 100
then
item.Body.Substring(0,100).ToLower.Contains("stop")
else
item.Body.Substring(0,len).ToLower.Contains("stop")

Something like this:

The simpler solution I see is to put assign with item.Body.Substring (0,100) .Contains (“stop”) in try catch. in catch you just give something like: item.Body.Contains (“stop”) just without substring.

Something like this:
image

2 Likes

This is an excellent post, do you have similar downloadable excel files on other areas? (apart from string manipulation)

1 Like

You are the best

1 Like

Nicely presented the content using graphic and list all possible option … Thanks you very much it really help me to refresh .

1 Like

Hello Can you please explain me below string

convert.ToDecimal(split(Split(Encounterbalance.ToString,“$”)(1),“)”)(0).ToString)<=10
thanks

Hi,
Explanation of the functions

The function takes the text between $ and ) from Encounterbalance string, converts a String to Decimal, checks the condition, and the result of the check is a Boolean variable. True or False.

3 Likes

@Adrian_Star Thanks a lot for great explanation

@Adrian_Star Also can you help me in configuring split method for this condition : example - ($7.26) or $7.26.

My condition works fine for $7.26 but it does not work for ($7.26) .

i have this condition right now used for splitting it ::

  1. downbalance = Split(BalancesListItem,“$”)(3) and
  2. split(Downbalance,“.”)(0)+“.”+split(Downbalance,“.”)(1).Substring(0,2)

Later i am converting downbalance to decimal and comparing it with greater than 10
convert.ToDecimal(Downbalance)>10.00 but i guess the bracket(7.26) is not being captured . I want to satisfy both the conditions

Try regex Pattern:

output = System.Text.RegularExpressions.Regex.Match(your_string,"((?<=\$)\b\d+.\d{1,2}\b|(?<=\$)\b\d+\b)").Value

Link: regex101: build, test, and debug regex
image

2 Likes

@Adrian_Star Thanks a ton!! it worked out but is there any way to fix from the split method I sent you

The code you provide shows that first you take 3rd element from the list containing amounts separated by $ sign, and then you process this element as required.

First you break the string after the period and take the part on the left. You add a period and finally break the string after the period, you take the part to the right in the first two characters only.

In this regard, Split has very precise requirements and is limited to any modifications.

The strength of a regex is that it is flexible.


You can use what I gave you and it should work:

downbalance = Split(BalancesListItem,"$")(3) and
downbalance = System.Text.RegularExpressions.Regex.Match(downbalance,"\d+[.]\d{2}").Value

Unless you have any other conditions that I don’t know about.

You can always show what your sample raw data looks like.


Your code alternative. Remove characters ( and ). Replace them to Nothing.

BalancesListItem = BalancesListItem.Replace(")","").Replace("(","")
downbalance = Split(BalancesListItem,"$")(3) and
downbalance = Split(Downbalance,".")(0) +"."+ Split(Downbalance,".")(1).Substring(0,2)
#Do some math
2 Likes

@Adrian_Star Thank you for this effort.! :clap: :innocent:

@Adrian_Star Thanks for the support on this and i have used your regex expresson.

Also, I am using your regex expression as “downbalance” variable and than evaluating like this further…

Split(BalancesListItem.ToString,“/”)(0).ToString.ToLower.Trim.Contains(“com”) or Split(BalancesListItem.ToString,“/”)(0).ToString.ToLower.Trim.Contains(“self”) or convert.ToDecimal(Downbalance)>=10.00

The next step in my process is to not complete the 10 dollar amounts which i have shown above as (convert.ToDecimal(Downbalance)>=10.00) but it is still solving the 10 dollar amounts. check my condition above which says if the converted downbalance is greater than or equal to 10 than do nothing furhter but it still does complete the 10 dollar ones. can you check what`s wrong ?