|
Hello,
Let's say I have the following variables:
string var1 = "5567 87865 4432 54535 6678 4435";
string var2 = "667 332 32 555677 4465 44444444";
string part_of_var1 = "4432 54535";
string xxx = String.Empty;
I want to have in xxx: 32 555677!
Notice that part_of_var1 contains the third and fourth parts of var1. I want to have 32 555677 in xxx var because it is the third and fourth parts of var2.
I hope it's clear!
Please help. Your help would be much appreciated.
|
|
|
|
|
I'm not really sure what you are trying to achieve here, but you can achieve what you want to do quite easily (and without hardcoding the result):
string[] splitter = var2.Split(' ');
if (splitter != null && splitter.Length > 4)
{
xxx = string.Format("{0} {1}", splitter[2], splitter[3]);
}
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi,
Thank you very much. The problem is I don't know the position at design or runtime. The only clue I have is the value of part_of_var1 variable.
So maybe I have to count spaces?
Thank again.
|
|
|
|
|
I think this will come close to what you want:
public void test() {
string var1 = "5567 87865 4432 54535 6678 4435";
string var2 = "667 332 32 555677 4465 44444444";
string part_of_var1 = "4432 54535";
string xxx=getPartsOfVar2(var1, var2, part_of_var1, ' ');
Console.WriteLine(xxx);
|
public string getPartsOfVar2(string var1, string var2, string part_of_var1, char sep) {
string[] arr1=var1.Split(sep);
string[] arr2=var2.Split(sep);
if (arr1.Length!=arr2.Length) throw some exception;
string[] arr1p=part_of_var1.Split(sep);
int partCount=arr1p.Length;
string[] arr2p=new string[partCount];
for(int i=0; i<partCount; i++) {
string part=arr1p[i];
int index=arr1.IndexOf(part);
if (index<0) throw some exception;
arr2p[i]=arr2[index];
}
return String.Join(sep, arr2p);
}
|
|
|
|
|
Hello Luc,
I'm getting the following errors when I'm trying to compile the code:
Error 1 No overload for method 'IndexOf' takes '1' arguments ccc.cs 626 19 ccc
Error 2 The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments ccc.cs 630 12 ccc
Error 3 Argument '1': cannot convert from 'char' to 'string' ccc.cs 630 24 ccc
|
|
|
|
|
I did not compile/run so you have to check the documentation and make small adaptations.
As an example Array.IndexOf seems to be a static method with two args, so study and
correct it.
|
|
|
|
|
Split var1 by part_of_var1, so that you get an array with two items. Count the number of spaces in each of them and put in s1 and s2. Split var2 by space, then join that array again except the first s1 items and the last s2 items.
Not dizzy yet...
---
single minded; short sighted; long gone;
|
|
|
|
|
Can you please give me some code, I'm really a beginner in C#.
|
|
|
|
|
I figured out exactly how to do it for you. Can't you at least make an attempt at writing the code yourself?
---
single minded; short sighted; long gone;
|
|
|
|
|
Ok sorry
I did what you said except:
"then join that array again except the first s1 items and the last s2 items."
How can I do that?
Thanks a lot.
|
|
|
|
|
I'm still trying but I can't figure out how to "join that array again except the first s1 items and the last s2 items."
|
|
|
|
|
Use the String.Join method, like this:
string result = String.Join(" ", arrayOfStrings, s1, arrayOfStrings.Length - s1 - s2)
---
single minded; short sighted; long gone;
|
|
|
|
|
|
Looking at MSDN overview of collections, I can not find anything about set.
How would you represent an unordered set of references in C#? The set will hold up to about million of references and I need fast Contains, Insert/Remove operations. I don't need intersection, union...
|
|
|
|
|
What type of references?
File references, dll references, .NET assemly references, memory references?
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios[ ^]
|
|
|
|
|
Hi,
there recently has been a CP article on sets (with set operations, which you dont want).
What you need is not really a set, it could be any kind of collection.
So try ArrayList (all .NET versions) or a List (if .NET >= 2.0)
|
|
|
|
|
I managed to put something together based on KeyedCollection. It's not perfect, but it get the job done:
<code>
public class Set<T> : KeyedCollection<T, T>
{
public Set()
: base( EqualityComparer<T>.Default, -1 )
{
}
public Set( IEqualityComparer<T> comparer )
: base( comparer, -1 )
{
}
public new void Add( T item )
{
if ( !this.Contains(item ) )
base.Add( item );
}
public void AddRange( IEnumerable<T> enumerable )
{
foreach ( T item in enumerable )
this.Add( item );
}
override protected T GetKeyForItem( T item )
{
return item;
}
}
</code>
|
|
|
|
|
I tried using images in my listview, but it doesn't work o_O
i added an imagelist, made it my listview's imagelist but i still don't see the image when i add an item (yes, i do pick an image index).
any solutions?
|
|
|
|
|
Have you set up Application.EnableVisualStyles by any chance? Try placing Application.DoEvents() immediately after it.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
My question is What is the exact syntax to connect? i mean its our first tym using IBM DB2 server and its kinda hard.
We've connected the DB2 to C# by the components but the exact syntax for opening a connection or closing or inserting an item to the table is a real question...thank you very much...
|
|
|
|
|
Hello, I'm new to attributes. Attributes seem to solve a problem I have with my data representation. However, how are attributes stored in a database?
Specifically, I'm using VS'03 to build a typed data schema, wherein I create and populate some elements. I can use the SqlDataAdapter to load/retrieve from the dB, no problem. Now, I add attributes to the schema, how do I get them into the dB and what is the representation?
Thanks.
NYC Traveler
|
|
|
|
|
I want to make like a schedule program and save the information with XML but im having some problems doing so. this is how i save the information:
<code>
<pre>
string s_FileName = Application.StartupPath + "\\List.xml";
XmlDocument docXML = new XmlDocument();
docXML.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Reminders>" +
"<Reminder>"+
"<Name>"+Add.s_Name+"</Name>"+
"<Date>"+Add.s_Date+"</Date>" +
"<Time>"+Add.s_Time+"</Time>" +
"<Comment>"+Add.s_Comment+"</Comment>"+
"</Reminder>"+
"</Reminders>");
docXML.Save(s_FileName);
</pre>
</code>
The problem is that it overwrites the previous text in the file every time, is there a way to append the text? and is there a better way to save the information? (this is my first time using XML and im realy a newbie o_O).
|
|
|
|
|
sharpiesharpie wrote: The problem is that it overwrites the previous text in the file every time, is there a way to append the text?
If you do not want to overwrite the file, then load the already existing file into the XmlDocument with help of the Load method.
docXML.Load(s_FileName);
Afterwards add all information you want by using the various CreateXX methods. Take a look at their documentation to see what exactly they are doing and how to use them.
sharpiesharpie wrote: and is there a better way to save the information?
If you want to persist a collection of some fix type in a XML file, I suggest the use of the XmlSerializer class. I've written an application for the management of birthdays that makes use of this. You can download the source code here: Birthday Manager[^]
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
wow, the xmlserializer is painfully complicated o_O, is there a simpler alternative? o_O
|
|
|
|
|
What does the XmlSerializer have to do with your original question? I mean, what you're doing is not XML serialization which is maybe why you're finding it hard. To serialize an object graph using actual XML serialization couldn't be any more easy (this is essentially how it works in any framework):
private void Serialize(object obj, Stream s)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer(s, obj);
} You can even customize XML serialization for your classes both declaratively and imperatively. Read Introduction to XML Serialization[^] in the .NET Framework. With attributes, declarative programming makes this way more easy than in other frameworks, like the JDK.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|