简体   繁体   中英

How to remove a portion of string

I want to remove word Test and Leaf from the specified string beginning only,not from the other side,so string Test_AA_234_6874_Test should be AA_234_6874_Test ,But when i use .Replace it will replace word Test from everywhere which i don't want.How to do it This is the code what i have done it

string st = "Test_AA_234_6874_Test";
st = st.Replace("Test_","");

You could use a regex to do this. The third argument of the regex replace method specifics how many times you want to replace.

string st = "Test_AA_234_6874_Test";
var regex = new Regex("(Test|Leaf)_");
var value = regex.Replace(st, "", 1);

Or if the string to replace only occurs on the start just use ^ which asserts the position at start of the string.

string st = "Test_AA_234_6874_Test";
var regex = new Regex("^(Test|Leaf)_");
var value = regex.Replace(st, "");

If you know that you allways have to remove the first 5 letters you can also use Substring which is more performant.

string st = "Test_AA_234_6874_Test";
var value = st.Substring(5, st.Length - 5);

Consider checking whether the string starts with "Start" and/or ends with "Trim" and decide the end and start positions you'd like to maintain. Then use Substring method to get only the portion you need.

public string Normalize(string input, string prefix, string suffix)
{
    // Validation
    int length = input.Length;
    int startIndex = 0;
    if(input.StartsWith(prefix))
    {
        startIndex = prefix.Length;
        length -= prefix.Length;
    }

    if (input.EndsWith (suffix))
    {
        length -= suffix.Length;
    }

    return input.Substring(startIndex, length);
}

Hope this helps.

The simplest way to do this is by using a Regular Expression like so.

using System;
using System.Text.RegularExpressions;
using System.Text;

namespace RegExTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = "Test_AA_234_6874_Test";
            var matchText = "Test";
            var replacement = String.Empty;

            var regex = new Regex("^" + matchText);

            var output = regex.Replace(input, replacement);

            Console.WriteLine("Converted String: {0}", output);

            Console.ReadKey();

        }
    }
}

The ^ will match text at the beginning of the string.

Use a regular expression.

        var str1 = "Test_AA_234_6874_Test";
        var str2 = "Leaf_AA_234_6874_Test";

        str1 = Regex.Replace(str1, "^Test", "");
        str2 = Regex.Replace(str2, "^Leaf", "");

Regex.Replace parameters are your input string (str1), the pattern you want to match, and what to replace it with, in this case a blank space. The ^ character means look at the start of the string, so something like "MyTest_AAAA_234_6874_Test" would stil return "MyTest_AA_234_6874_Test".

string wordToRemoveFromBeginning = "Test_";
int index = st.IndexOf(wordToRemoveFromBeginning);

string cleanPath = (index < 0) ? st : st.Remove(index, 
    wordToRemoveFromBeginning.Length);

I am gonna use some very simple code here

string str = "Test_AA_234_6874_Test";
string substring = str.Substring(0, 4);
if (substring == "Test" || substring == "Leaf")
{
   str= str.Remove(0, 5);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM