473,387 Members | 1,574 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,387 software developers and data experts.

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 3462
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(str.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.com

"Rhonda Tipton" <Rt*******@excite.com> wrote in message
news:Lf******************@tornado.texas.rr.com...
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.com> wrote in
message news:u9****************@TK2MSFTNGP12.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(str.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.com

"Rhonda Tipton" <Rt*******@excite.com> wrote in message
news:Lf******************@tornado.texas.rr.com...
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.com

"Rhonda Tipton" <Rt*******@excite.com> wrote in message
news:Tx******************@tornado.texas.rr.com...
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.com> wrote
in message news:u9****************@TK2MSFTNGP12.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(str.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.com

"Rhonda Tipton" <Rt*******@excite.com> wrote in message
news:Lf******************@tornado.texas.rr.com...
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.com> wrote in
message news:un**************@TK2MSFTNGP14.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.com

"Rhonda Tipton" <Rt*******@excite.com> wrote in message
news:Tx******************@tornado.texas.rr.com...
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.com> wrote
in message news:u9****************@TK2MSFTNGP12.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(str.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.com

"Rhonda Tipton" <Rt*******@excite.com> wrote in message
news:Lf******************@tornado.texas.rr.com...
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
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...
11
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...
17
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...
1
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...
9
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...
1
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...
6
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...
13
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...
6
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...
0
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.