473,563 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Operation in C#

Hello.

I am trying to do a substring or something like that to extract the middle
portion of a string.

12345 TEST FACILITY 1_Letter 1A
12346 TEST FACILITY 2_Letter 1A

I would like to be able to extract the part after the number and before the
_.

TEST FACILITY 1
TEST FACILITY 2

I am new to C# and still learning, so any help is greatly appreciated.

Rt
Nov 17 '05 #1
4 3468
Rhonda,

Do you know that the number always comes first, and has a space after
it? If so, I would get the index of the first space, and the first
underscore, and then get the substring using those two indexes, like this:

// Assuming str has your string, you could do:
// The index of the space.
int spaceIndex = str.IndexOf(' ');
int ulIndex = str.IndexOf('_' );

// Get the substring based on the indexes.
subStr = str.Substring(s tr.IndexOf(' ') + 1, ulIndex - spaceIndex - 1);

This assumes there is always a single space before the beginning of the
section you want to get, and an underscore immediately following it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rhonda Tipton" <Rt*******@exci te.com> wrote in message
news:Lf******** **********@torn ado.texas.rr.co m...
Hello.

I am trying to do a substring or something like that to extract the middle
portion of a string.

12345 TEST FACILITY 1_Letter 1A
12346 TEST FACILITY 2_Letter 1A

I would like to be able to extract the part after the number and before
the _.

TEST FACILITY 1
TEST FACILITY 2

I am new to C# and still learning, so any help is greatly appreciated.

Rt

Nov 17 '05 #2
Nicholas,
Yes there is always a 5 digit number first in my situation. I will try
this. Thanks for the quick response.

Rhonda
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:u9******** ********@TK2MSF TNGP12.phx.gbl. ..
Rhonda,

Do you know that the number always comes first, and has a space after
it? If so, I would get the index of the first space, and the first
underscore, and then get the substring using those two indexes, like this:

// Assuming str has your string, you could do:
// The index of the space.
int spaceIndex = str.IndexOf(' ');
int ulIndex = str.IndexOf('_' );

// Get the substring based on the indexes.
subStr = str.Substring(s tr.IndexOf(' ') + 1, ulIndex - spaceIndex - 1);

This assumes there is always a single space before the beginning of the
section you want to get, and an underscore immediately following it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rhonda Tipton" <Rt*******@exci te.com> wrote in message
news:Lf******** **********@torn ado.texas.rr.co m...
Hello.

I am trying to do a substring or something like that to extract the
middle portion of a string.

12345 TEST FACILITY 1_Letter 1A
12346 TEST FACILITY 2_Letter 1A

I would like to be able to extract the part after the number and before
the _.

TEST FACILITY 1
TEST FACILITY 2

I am new to C# and still learning, so any help is greatly appreciated.

Rt


Nov 17 '05 #3
Rhonda,

If you know there is always a five digit number, followed by a space,
you can reduce this code to:

// Assuming str has your string, you could do:
// The index of the space.
int ulIndex = str.IndexOf('_' );

// Get the substring based on the indexes.
subStr = str.Substring(6 , ulIndex - 6);
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rhonda Tipton" <Rt*******@exci te.com> wrote in message
news:Tx******** **********@torn ado.texas.rr.co m...
Nicholas,
Yes there is always a 5 digit number first in my situation. I will try
this. Thanks for the quick response.

Rhonda
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:u9******** ********@TK2MSF TNGP12.phx.gbl. ..
Rhonda,

Do you know that the number always comes first, and has a space after
it? If so, I would get the index of the first space, and the first
underscore, and then get the substring using those two indexes, like
this:

// Assuming str has your string, you could do:
// The index of the space.
int spaceIndex = str.IndexOf(' ');
int ulIndex = str.IndexOf('_' );

// Get the substring based on the indexes.
subStr = str.Substring(s tr.IndexOf(' ') + 1, ulIndex - spaceIndex - 1);

This assumes there is always a single space before the beginning of
the section you want to get, and an underscore immediately following it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rhonda Tipton" <Rt*******@exci te.com> wrote in message
news:Lf******** **********@torn ado.texas.rr.co m...
Hello.

I am trying to do a substring or something like that to extract the
middle portion of a string.

12345 TEST FACILITY 1_Letter 1A
12346 TEST FACILITY 2_Letter 1A

I would like to be able to extract the part after the number and before
the _.

TEST FACILITY 1
TEST FACILITY 2

I am new to C# and still learning, so any help is greatly appreciated.

Rt



Nov 17 '05 #4
Cool.... that works perfect for me. Thanks again for your help.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:un******** ******@TK2MSFTN GP14.phx.gbl...
Rhonda,

If you know there is always a five digit number, followed by a space,
you can reduce this code to:

// Assuming str has your string, you could do:
// The index of the space.
int ulIndex = str.IndexOf('_' );

// Get the substring based on the indexes.
subStr = str.Substring(6 , ulIndex - 6);
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rhonda Tipton" <Rt*******@exci te.com> wrote in message
news:Tx******** **********@torn ado.texas.rr.co m...
Nicholas,
Yes there is always a 5 digit number first in my situation. I will try
this. Thanks for the quick response.

Rhonda
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:u9******** ********@TK2MSF TNGP12.phx.gbl. ..
Rhonda,

Do you know that the number always comes first, and has a space after
it? If so, I would get the index of the first space, and the first
underscore, and then get the substring using those two indexes, like
this:

// Assuming str has your string, you could do:
// The index of the space.
int spaceIndex = str.IndexOf(' ');
int ulIndex = str.IndexOf('_' );

// Get the substring based on the indexes.
subStr = str.Substring(s tr.IndexOf(' ') + 1, ulIndex - spaceIndex - 1);

This assumes there is always a single space before the beginning of
the section you want to get, and an underscore immediately following it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rhonda Tipton" <Rt*******@exci te.com> wrote in message
news:Lf******** **********@torn ado.texas.rr.co m...
Hello.

I am trying to do a substring or something like that to extract the
middle portion of a string.

12345 TEST FACILITY 1_Letter 1A
12346 TEST FACILITY 2_Letter 1A

I would like to be able to extract the part after the number and before
the _.

TEST FACILITY 1
TEST FACILITY 2

I am new to C# and still learning, so any help is greatly appreciated.

Rt



Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1833
by: Neil Schemenauer | last post by:
The title is perhaps a little too grandiose but it's the best I could think of. The change is really not large. Personally, I would be happy enough if only %s was changed and the built-in was not added. Please comment. Neil PEP: 349 Title: Generalised String Coercion
11
1525
by: ALI-R | last post by:
why c# function for string manupolation is not as strong as other languages ,for extracting a substring from a middle of another string ,I have a lot of problems. Any suggestion or I am in the wrong spot. Thanks
17
4646
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if(...
1
2746
by: varunhome | last post by:
Hi, I want to check for the absence of a string in regular expression. For example, if the string is "Error opening file: Permission denied. Aborting.", I want to check for absence of the string "Permission". I have tried many possibilities without success. Thanks for any help, Varun Sud
9
7114
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not...
1
18149
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it is serialized correctly, and the server returns a response (I've captured the response and it's correct!) but when the .NET deserialize this...
6
6093
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or more minutes. 'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++ Dim col As New Scripting.Dictionary Dim ii As Integer = 0
13
3199
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I didn't found it yet. help please thank you Freaker85
6
7459
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my methods in CLI return System::String^. I originally just gcnew'd System::String^ passing in the c_str(). But I can't really have as many gcnew's as...
0
987
by: Bart Kastermans | last post by:
|    def __str__ (self): I did some timing of operations involved. Doing this I found that the operation below to get a string representation for trees was in fact not quadratic. The final nail in the coffin of this comment is now at: http://kasterma.wordpress.com/2008/06/30/strings-versus-lists/ (I put it there because the main part is...
0
7658
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7877
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7631
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6238
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2077
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
912
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.