Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im trying to remove a space from a string.I have tried trim methods but it does not give me what i want.Actual the string contains a path
eg
http://localhost:23766/WebSite2/NewFolder1/Division of ICT/Dept of MIS and Operations/Software Request Form/Samabula /HR Policy Review for HRC_RC_3052012_final.pdf

There isa white space between Samabula and /HR..... which i need to remove.I can not use the index trim such as string.trim(x,x)since the string is determine dynamically.

Any help would be of great help.
Posted

Hi,

As per your explanation it is very easy to resolve your issue, i hope you do not want to break the path so space in file name/folder name should not be changed. right ?

Please check below code,

C#
string str = "yourpath";
str.Replace(" /","/");
str.Replace("/ ","/");


Thanks
-Amit Gajjar
 
Share this answer
 
simple..
Its obvious that space should not be allowed before "/" in paths.
C#
mystring.Replace(" /","/");


Here am replacing space + "/" with "/". So the space preceding symbol "/" will be removed in the string wherever it is/
 
Share this answer
 
v2
HTML
Use Below code for your problem.. it will definitely solve your problem.

C#
string str = @"http://localhost:23766/WebSite2/NewFolder1/Division of ICT/Dept of MIS and Operations/Software Request Form/Samabula /HR Policy Review for HRC_RC_3052012_final.pdf";
string[] strsplit = str.Split('/');
str =null;
foreach (string item in strsplit) { str = str + item.Trim() + "/"; }
 
Share this answer
 
Hi Dear,

You can try it:

C#
static void Main(string[] args)
       {
            StringToTrim();
            Console.ReadLine();
       }
       private static void StringToTrim()
       {
           String str = "My Name Is Niranjan Kumar";
           string delimeter = " ";
           String[] Result = str.Split(new[] { delimeter }, StringSplitOptions.None);
           for (int i = 0; i < Result.Length; i++)
           {
               Console.Write(Result[i]);
           }
       }
 
Share this answer
 
You can try this
C#
 public static int Main(string[] args)
    {
        Console.WriteLine(URL.PrepareURL("http://localhost:23766/WebSite2/NewFolder1/Division of ICT/Dept of MIS and Operations/Software Request Form/Samabula /HR Policy Review for HRC_RC_3052012_final.pdf"));
        Console.ReadLine();
        return 0;
    }
}
public static class URL
{
    static readonly Regex feet = new Regex(@"([0-9]\s?)'([^'])", RegexOptions.Compiled);
    static readonly Regex inch1 = new Regex(@"([0-9]\s?)''", RegexOptions.Compiled);
    static readonly Regex inch2 = new Regex(@"([0-9]\s?)""", RegexOptions.Compiled);
    static readonly Regex num = new Regex(@"#([0-9]+)", RegexOptions.Compiled);
    static readonly Regex dollar = new Regex(@"[$]([0-9]+)", RegexOptions.Compiled);
    static readonly Regex percent = new Regex(@"([0-9]+)%", RegexOptions.Compiled);
    static readonly Regex sep = new Regex(@"[\s_/\\+:.]", RegexOptions.Compiled);
    static readonly Regex empty = new Regex(@"[^-A-Za-z0-9]", RegexOptions.Compiled);
    static readonly Regex extra = new Regex(@"[-]+", RegexOptions.Compiled);

    public static string PrepareURL(string str)
    {
        str = str.Trim().ToLower();
        str = str.Replace("&", "and");

        str = feet.Replace(str, "$1-ft-");
        str = inch1.Replace(str, "$1-in-");
        str = inch2.Replace(str, "$1-in-");
        str = num.Replace(str, "num-$1");

        str = dollar.Replace(str, "$1-dollar-");
        str = percent.Replace(str, "$1-percent-");

        str = sep.Replace(str, "-");

        str = empty.Replace(str, string.Empty);
        str = extra.Replace(str, "-");

        str = str.Trim('-');
        return str;
    }
}
 
Share this answer
 
try this Code friend:

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        string str = "http://localhost:23766/WebSite2/NewFolder1/Division of ICT/Dept of MIS and Operations/Software Request Form/Samabula /HR Policy Review for HRC_RC_3052012_final.pdf";
        string[] str1 = str.Split(' ');
        Label1.Text = "";
        for (int i = 0; i < str1.Length; i++)
        {
            Label1.Text = Label1.Text + str1[i];
        }

    }


regards
sarva
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900