سؤال

I have a string like this: "Instructions: Hello : How are you"

And i want result string like this "Hello : How are you"

In string after "Instructions:" I can add as many as sentences starting with colon ":" like i start : How are you

code i am using

  string[] MyString = oldstring.Split(':');
  if (MyString != null && MyString .Length > 1)
  {
       oldstring = MyString[1].Replace(";", "").TrimStart();
  }

My code trims "Instructions:" and all other sentences after second colon ":" i.e after Hello

I want such only "Instructions:" word will be trimmed and return whole string regardless of number of sentences and colon it contains.

Please Suggest Logic and problem is that I can use only Trim() Function

i am using C# and ASP.NET for language reference.

هل كانت مفيدة؟

المحلول

How does the following not do what you want? (The question is vague to me).

string[] MyString = oldstring.Split(':');
if (MyString != null && MyString.Length > 1)
{
    MyString[0] = MyString[0].Trim();
}
oldstring = string.Join(":",MyString);

This will split oldstring into an array, trim the first word, then join the array together into a string again. The first word is trimmed, nothing else is touched.

After reading your question again, I'm starting to think that you're confusing what "Trim" means in this case. Trimming means removing whitespace. It looks like you're talking about removing the first word. If that's the case, change MyString[0] = MyString[0].Trim(); to MyString[0] = ""; and run the rest of the code as shown. This will remove the first word of the array and return the rest as a string.

نصائح أخرى

This will work for different commands relevant to particular words.

        if (oldstring.IndexOf(':') > -1)
        {
            oldstring = oldstring.Substring(oldstring.IndexOf(':') + 1).Trim();
        }

This is done using Trim only.

string oldstring = "Instructions:Hello : How are you";
oldstring = oldstring.TrimStart("Instructions: ".ToArray());

Based on your requirement you to ToUpper() or ToLower().

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top