2

C# のコード ビハインドには、次のコードがあります。www の最初の出現のみが置換されるように置換を変更するにはどうすればよいですか? たとえば、ユーザーが www.testwww.com と入力した場合、testwww.com として保存する必要があります。現在、以下のコードに従って、www.com として保存されます (substr コードによると推測されます)。助けてください。前もって感謝します。

private string FilterUrl(string url)
    {
        string lowerCaseUrl = url.ToLower();
        lowerCaseUrl = lowerCaseUrl.Replace("http://", string.Empty).Replace("https://", string.Empty).Replace("ftp://", string.Empty);
        lowerCaseUrl = lowerCaseUrl.Replace("www.", string.Empty);

        string lCaseUrl = url.Substring(url.Length - lowerCaseUrl.Length, lowerCaseUrl.Length);
        return lCaseUrl; 
    }
4

5 に答える 5

3

アリーが提案したように。を使用する方がはるかに優れていSystem.Uriます。これは、必要に応じて先頭の www も置き換えます。

private string FilterUrl(string url)
{
    Uri uri = new UriBuilder(url).Uri; // defaults to http:// if missing
    return Regex.Replace(uri.Host, "^www.", "") + uri.PathAndQuery;
}

編集: 末尾のスラッシュは PathAndQuery プロパティによるものです。パスがない場合は、スラッシュのみが残ります。別の正規表現置換または文字列置換を追加するだけです。これが正規表現の方法です。

return Regex.Replace(uri.Host, "^www.", "") + Regex.Replace(uri.PathAndQuery, "/$", "");
于 2012-12-07T19:41:33.533 に答える
0

クールな静的メソッドを思いついた。最初のx個のオカレンスを置き換えるためにも機能する。

public static string ReplaceOnce(this string s, string replace, string with)
{
    return s.ReplaceCount(replace, with);
}

public static string ReplaceCount(this string s, string replace, string with, int howManytimes = 1)
{
    if (howManytimes < 0) throw InvalidOperationException("can not replace a string less than zero times");
    int count = 0;
    while (s.Contains(replace) && count < howManytimes)
    {
        int position = s.IndexOf(replace);
        s = s.Remove(position, replace.Length);
        s = s.Insert(position, with);
        count++;
    }
    return s;
}

ReplaceOnceは必要ではなく、単純化するだけですこのように呼んでください:

string url = "http://www.stackoverflow.com/questions/www/www";

var urlR1 - url.ReplaceOnce("www", "xxx");
// urlR1 = "http://xxx.stackoverflow.com/questions/www/www";


var urlR2 - url.ReplaceCount("www", "xxx", 2);
// urlR2 = "http://xxx.stackoverflow.com/questions/xxx/www";

注:これは、記述されているとおり大文字と小文字が区別されます

于 2012-12-07T19:45:28.850 に答える
0

このReplaceメソッドは、文字列のすべての内容を変更します。IndexOfメソッドを使用して削除する部分を見つけ、Remove文字列のメソッドを使用して削除する必要があります。次のようなことを試してください:

//include the namespace
using System.Globalization;


private string FilterUrl(string url)
{
    // ccreate a Comparer object.
    CompareInfo myCompare = CultureInfo.InvariantCulture.CompareInfo;

    // find the 'www.' on the url parameter ignoring the case.
    int position = myCompare.IndexOf(url, "www.", CompareOptions.IgnoreCase);

    // check if exists 'www.'  on the string.
    if (position > -1)
    {
      if (position > 0)
         url = url.Remove(position - 1, 5);
      else
         url = url.Remove(position, 5);
    }

    //if you want to remove http://, https://, ftp://.. keep this line
    url = url.Replace("http://", string.Empty).Replace("https://", string.Empty).Replace("ftp://", string.Empty);

    return url;
}   

編集

コードに文字列の一部を削除する部分がありました。「www.」だけを削除したい場合 「http://」、「https://」、「ftp://」、このコードを見てください。

また、このコードは、url パラメーターと、大文字と小文字が区別された場合に見つかったものを比較するときに、大文字と小文字を区別しません。

于 2012-12-07T19:34:58.583 に答える
0

indexOf(string) を使用して最初の出現を見つけることをお勧めします。

編集:誰かが私を打ち負かしました;)

于 2012-12-07T19:35:37.410 に答える
0

Felipeが提案したようにIndexOfを使用するか、ローテクな方法で行うことができます..

 lowerCaseUrl = lowerCaseUrl.Replace("http://", string.Empty).Replace("https://", string.Empty).Replace("ftp://", string.Empty).Replace("http://www.", string.Empty).Replace("https://www.", string.Empty)

あなたが達成しようとしていることを知りたいです。

于 2012-12-07T19:39:20.657 に答える