| |

VerySource

 Forgot password?
 Register
Search
View: 762|Reply: 7

Character interception (a bit difficult)

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-12 22:30:01
| Show all posts |Read mode
How to repeatedly remove such strings "aa, bb, cc, dd, aa, bb, cc, dd" into "aa, bb, cc, dd"
It's very difficult, I hope the experts will help me, thank you
Reply

Use magic Report

0

Threads

64

Posts

45.00

Credits

Newbie

Rank: 1

Credits
45.00

 China

Post time: 2020-6-7 19:15:02
| Show all posts
In "aa, bb, cc, dd, aa, bb, cc, dd", go to the string before the first "," and loop to find the next one, and replace "aa," or "aa" with "", you will get a new string, repeat the above, find the next
Reply

Use magic Report

0

Threads

32

Posts

20.00

Credits

Newbie

Rank: 1

Credits
20.00

 China

Post time: 2020-6-8 11:15:01
| Show all posts
protected void Page_Load( object sender, EventArgs e)
  {
    string a = "aa,bb,cc,dd,aa,bb,cc,dd";
    string[] arr = a.Split(',');
    string[] b = RemoveDups(arr, true);
    for (int i = 0; i <b.Length; i++)
    {
      Response.Write("<li>" + b[i]);
    }
  }
  public string[] RemoveDups( string[] items, bool sort)
  {
    ArrayList noDups = new ArrayList();
    for (int i = 0; i <items.Length; i++)
    {
      if (!noDups.Contains(items[i].Trim()))
      {
        noDups.Add(items[i].Trim());
      }
    }
    if (sort) noDups.Sort();
    string[] uniqueItems = new String[noDups.Count];
    noDups.CopyTo(uniqueItems);
    return uniqueItems;
  }
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-6-8 14:30:02
| Show all posts
string str = "aa,bb,cc,dd,aa,bb,cc,dd";
The
ArrayList al = new ArrayList(str.Split(','));
al.Sort();
The
for(int i=1;i<al.Count;i++)
{
if(al[i].ToString() == al[i-1].ToString())
{
al.RemoveAt(i);
i--;
}
}


str = "";

for(int i=0;i<al.Count;i++)
{
str += al[i].ToString()+",";
}
The

str =str.Substring(0,str.Length-1);
Response.Write(str);
Reply

Use magic Report

0

Threads

24

Posts

20.00

Credits

Newbie

Rank: 1

Credits
20.00

 China

Post time: 2020-6-8 22:30:01
| Show all posts
Try:

using System.Text;
StringBuilder sb=new StringBuilder();
string str="aa,bb,cc,dd,aa,bb,cc,dd";
string[] tempstr=str.Split(',');
for(int i=0;i<tempstr.Length;i++)
{
  string tmp=tempstr[i];
  int flag=0;
  for(int j=0;j<tempstr.Length;j++)
  {
     if(tmp==tempstr[j])
     {
        flag=1;
        break;
     }
  }
  if(flag==0)
  {
     sb.Append(tmp);
     sb.Append(",");
   }
}
return sb.ToString();
Reply

Use magic Report

0

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 LAN

Post time: 2020-6-10 00:15:01
| Show all posts
string a="aa,bb,cc,dd,aa,bb,cc,dd";
string[] strs=a.Split(',');
Hashtable ht=new Hashtable();
foreach(string str in strs)
if(!ht.Contains(str))
ht.Add(str,"");
string strRen="";
IDictionaryEnumerator ide=ht.GetEnumerator();
ide.Reset();
while(ide.MoveNext())
{
strRen+=(string)ide.Key+",";
}//Give you a fun way to write
Reply

Use magic Report

0

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 LAN

Post time: 2020-6-10 10:45:01
| Show all posts
string a="aa,bb,cc,dd,aa,bb,cc,dd";
string[] strs=a.Split(',');
Hashtable ht=new Hashtable();
string strRen="";
foreach(string str in strs)
if(!ht.Contains(str))
{
ht.Add(str,"");
strRen+=str+",";
}
//It turns out that
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-13 09:45:01
| Show all posts
Use the split function to generate an array... Greatly written in detail..

It doesn't have to be compared one by one.. In the collection framework. Some do not allow duplicate values. Hashtable is a good example.

I don’t remember which of the HashMap HashSet does not allow duplicates...
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list