473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Im using while and substring to get specific text from text file but not working:

All the solutions here are great.

But for now im keeping on my own code cuz i want also to learn the way i started to do it.

So what i did so far is:


Expand|Select|Wrap|Line Numbers
  1. private void inloop()
  2.         {
  3.             while (true)
  4.             {
  5.                 y = @"d:\test.txt";
  6.                 readers = new StreamReader(y);
  7.                 string ff = readers.ReadToEnd();
  8.                 index = ff.IndexOf("1", index+1);
  9.                 if (index == -1)
  10.                 {
  11.                     break;
  12.                 }
  13.                 index1 = ff.IndexOf("3", index);
  14.                 string g = ff.Substring(index,index1-index+1);
  15.                 substring1 = ff.Substring(0, index);
  16.                 substring2 = ff.Substring(index + g.Length, ff.Length - (substring1.Length + g.Length));
  17.                 string test = substring1 + substring2;
  18.                 MessageBox.Show("Results : " + test);
  19.             }
  20.         }
  21.  
So i mgetting g all the time 123 whenever there 123 in the text file im getting it.

This is the text in the text file: "dann123ylip123sharon123daniel"



The problem is with substring2 > substring2 = ff.Substring(index + g.Length, ff.Length - (substring1.Length + g.Length));
In substring1 im getting the first part of the name wich is "dann" but then on the substring2 im getting > "ylip123sharon123daniel"

Wich isnt good cuz i want that test in the end will contain > "dannylipsharondaniel"

So what am i doing wrong here in substring2 ?


And please keep on my own code since i want to learn how to do it the way i started.

Thanks.
Mar 15 '11 #1
5 2606
GaryTexmo
1,501 Expert 1GB
I'm having a lot of trouble understanding what you're asking for here. I'm going to guess english isn't your first language? Please bear with me in trying to understand what the problem is.

I gather that you're looking for a certain output given a certain input but I can't honestly make heads or tails of what you're trying to do. Are you trying to strip all instance of 123 from the source string and return the original string without the 123 parts, or are you trying to break it up into separate strings?

Perhaps it would be easier if you gave an example of input and what the final, expected output would be.
Mar 15 '11 #2
GaryTexmo what im trying to do is to find all the places have 123 in the text file ( source string ) then im trying to strip it all so in the end ill left with the text only without the 123.

Its like doing CUT for the 123.

I forgot to show that in the class level im using streareader and readtoend() to read all the file content.

So if the file content wich is the source contain now:
"dann123ylip123sharon123daniel" what im trying to do is to strip(cut) all the places with 123 so in the end ill have a new string containing "dannylipsharondaniel"

After that when ill find a solution/repair for this problem i want to take text from another text file and implent it instead the 123 i mean in each place i cut the 123 i want to put something else so the source string in the end will look like something:

"dann456ylip456sharon456daniel"
But thats dosent matter now.

For now i want to get a result that all 123 places are cut striped and to end with "dannylipsharondaniel"

Thanks.
Mar 15 '11 #3
GaryTexmo
1,501 Expert 1GB
Thanks for clearing that up. I thought that might have been what you wanted, but I wanted to make sure before I tried to help you.

You mentioned you wanted to do the code for this on your own, so it sounds like you're trying to figure out how to solve this problem. I can totally respect that but I'll still mention that there are built in functions for the .NET string class that will do this for you. You're already using functions like SubString and IndexOf, so at that point you might want to consider string.Remove in conjunction with string.IndexOf.

I'll let you chew on that for a while and if you need help or want to explore the concept further, let me know :)
Mar 16 '11 #4
Gary i didnt success please show me how to do it with my own code.

But now instead of test file with one line "danny123ylip123sharon123lipman" inside the file.

I have a very big file wich is xml but i dont want to work on it as xml file i want to do it as i did so far.
So i can now get from the xml file all thew text inside without the tags and other stuff only the text i need i did it like this:


Expand|Select|Wrap|Line Numbers
  1. string substring1;
  2.             string substring2;
  3.             string test;
  4.             w=new StreamWriter(@"d:\loca_german.xml");
  5.             while (true)
  6.             {
  7.                 index = tt.IndexOf("Text value", index + 1);
  8.                 if (index == -1)
  9.                 {
  10.                     break;
  11.                 }
  12.                 int t = tt.IndexOf("\"", index);
  13.                 int y = tt.IndexOf(">",t);
  14.                 g = tt.Substring(t,y-t);
  15.  
So this part is working now in g when it will get to the end of the file after looping ill have all the text i need from the xml in G

Now what i want to do is like CUT. As i told you before.
After i have the text i need in G i want to move out all the text in G from the the xml file and end with a string that will contain all the tags and all the text and all the rest without the G string.

Like doing a reverse cut i explained you earlier and you understood.

I tried to use after getting the text in g inside the loop i tried to use two new strings substring1 and substring2 but it didnt work good.

I tried to do somethingl ike:

Expand|Select|Wrap|Line Numbers
  1. substring1 = tt.Substring(0, index);
  2. substring2 = tt.Substring(index + g.Length, ff.Length - (index + g.Length));
  3.  
Then i use a new string and did :

Expand|Select|Wrap|Line Numbers
  1. string test = substring1+substring2;
  2.  
And i put it all in a listbox1
But it didnt work as i wanted.

Please how me how to do it using this code i did so far.

Thanks a lot.
Mar 17 '11 #5
NeoPa
32,556 Expert Mod 16PB
Chocolade, You say much in your latest post, but frankly it's very hard to understand some of it. I appreciate it's not easy to express yourself clearly in a foreign language, but if you could go over your post carefully and fix any problems you see (there are many typos - which don't help when someone is already struggling to understand what you're trying to say), then Gary may be in a better position to help you. I know he wants to.

Please remember in future that the care you take when posting is very important, and helps people who want to help you, to do so. It applies to smaller posts too of course, but the larger the post, the more difficult it is to follow (even when expressed perfectly in many cases).
Mar 17 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: mike | last post by:
I have a script that is not rendering the textwrap in svg properly. //does not work ..... var svgLand = svgObj.getElementById("NarrDisplay"); mytextwrap=...
6
by: pankaj.khandelwal | last post by:
Hi, How can I bundle a text/gif file in the shared object. Basically I want to read the content of this text file but I cannot have it seperately on the disc. Any clues. ? Pankaj
1
by: David Dvali | last post by:
How can I load text from file to TextBox?
2
by: Samuel R. Neff | last post by:
What options are available for doing full-text searches of database data without using a database-specific full-text engine? The only option I've found is Google's Search Appliance but it's an...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
0
by: Ahmed A. | last post by:
This will be very helpfull for many! Using RichTextBox Read/Write Unicode File http://www.microsoft.com/indonesia/msdn/wnf_RichTextBox.as p Private Function ReadFile(ByVal myfile As String)...
4
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic">...
0
by: Sasie7679 | last post by:
Dear All, We have a requirement to open any file using a specific application and showing it within a activex control. For example, bmp file can be shown in Internet explorer, paint brush, photo...
0
by: PRITPAL | last post by:
Hi There, I want a code for Saving and Reading formatted text (RTF File) in MS Access using ole Objects, i want to save 20 such records in DB using VB 6.0. Plz Help
2
by: =?Utf-8?B?TWlrZSBE?= | last post by:
Is it possible to retreive groups from a sub-OU by using substring? I need to be able to loop over the OUs of a OU to see if any have a sub-OU whose Name ends with 'Security Groups' and output the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.