Substring dynamic if the end is word

Hello all,

When i do substring how can i change the value dynamically if there is word not completed at the end

For example Address.Substring(11) from Stockholm sweden i want to change 11 to 8 so i done split the word sweden!

Thanks

by using replace, address.replace(“old value”,“new value”)

Hi,

If you want to extract the last word, can you try the following expression?

yourString.Split({" "c}).Last()

image

Regards,

thanks i want to extract of the substring in the middle of the word not the last if we have 3 words
and my field is only 20 strings and a have to write down from 0 to 20 but if from 17 to 21 is one word then i move to substring from 0 to 17…
is that possible ?

Hi,

Can you try the following expression?

String.Concat(yourString.Take(yourString.Select(Function(c,i) if((c=" "c  OrElse i=yourString.Length-1) AndAlso i<20,i,-1)).Max()))

Regards,

Sorry is this in VB?

i have very low knowledge in vb

seems complaining on the if statment

String.Concat(Companyname_IN.Take(Companyname_IN.Select(Function(c,i) if((c=" "c  Or Else i=Companyname_IN.Length-1) AndAlso i<20,i,-1)).Max()))

image

Thanks everyone now i have dynamic index

string input = "This is my sting to testindex34 oneword";
        int desiredIndex = 34;

        if (input.Length > desiredIndex)
        {
            int lastIndex = input.LastIndexOf(' ', desiredIndex);
            string result = input.Substring(0, lastIndex);
            Console.WriteLine(result);
			
			Console.WriteLine(desiredIndex.ToString());
			Console.WriteLine(lastIndex.ToString());
        }
        else
        {
            Console.WriteLine(input);
        }

Hi,

Your project is C#. How about the following expression because the above is for VB?

 String.Concat(yourString.Take(yourString.Select((c,i)=> (c==' '  || i==yourString.Length-1) && i<20 ? i :-1).Max()))

Regards,

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