1

I hava a specific string that I want to replace

string gerneralRootPath = docTab.Rows[0]["URL"].ToString();
string documentName = docTab.Rows[0]["NAME"].ToString();

var connectNamesAndURL = new StringBuilder(gerneralRootPath);
connectNamesAndURL.Remove(30,20);
connectNamesAndURL.Insert(30, documentName);
gerneralRootPath = connectNamesAndURL.ToString();

The output of gerneralRootPath is "Documents/Z_Documentation/PDF/sales.2010+Implementation+Revised+Feb10.pdf"

The output of is documentName is "doc123"

My gole is to remove everything after /PDF/.. so that final string looks like this Documents/Z_Documentation/PDF/doc123

So how can I remove everything after the /PDF/..

5 Answers 5

1

Try this

string gerneralRootPath = "Documents/Z_Documentation/PDF/sales.2010+Implementation+Revised+Feb10.pdf";
gerneralRootPath = gerneralRootPath.Remove(gerneralRootPath.IndexOf("PDF") + 3);
gerneralRootPath = gerneralRootPath +"/"+documentName ;
Sign up to request clarification or add additional context in comments.

Comments

0

You can achieve this using String.Split() function:

string input = "Documents/Z_Documentation/PDF/sales.2010+Implementation+Revised+Feb10.pdf";
string output = input.Split(new string[] { "/PDF/" }, StringSplitOptions.None).First() + "/PDF/doc123";

Comments

0
using System.IO;  

string result = gerneralRootPath.Replace(Path.GetFileName(gerneralRootPath), documentName);

With Path.GetFileName (from System.IO) you get your filename:

sales.2010+Implementation+Revised+Feb10.pdf

The result is:

Documents/Z_Documentation/PDF/doc123

Comments

0

please find the sample code

int i = gerneralRootPath.IndexOf("/PDF/");

 if (i >= 0) gerneralRootPath = gerneralRootPath.Substring(0,i+5);

i hope this will help you....

Comments

0

This is an example:

class Program
{
    static string RemoveAfterPDF(string gerneralRootPath)
    {
        string pdf = "PDF";
        int index = gerneralRootPath.IndexOf(pdf);
        return gerneralRootPath.Substring(0, index + pdf.Length);
    }

    public static void Main()
    {
        string test = RemoveAfterPDF("Documents/Z_Documentation/PDF/sales.2010+Implementation+Revised+Feb10.pdf");
    }
}

Edit This is better and much more reusable example:

class Program
{
    static string RemoveAfter(string gerneralRootPath, string removeAfter)
    {
        string result = string.Empty;
        int index = gerneralRootPath.IndexOf(removeAfter);

        if (index > 0)
            result = gerneralRootPath.Substring(0, index + removeAfter.Length);

        return result;
    }

    public static void Main()
    {
        string test = RemoveAfterPDF("Documents/Z_Documentation/PDF/sales.2010+Implementation+Revised+Feb10.pdf", "PDF");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.