Click here to Skip to main content
16,004,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody!

Im new in c# and i have a trouble with split text.

i have a example:

String: STR_LOCAL_ABS_CERAMIUM_A21

i want get text "CERAMIUM" between "STR_LOCAL_ABS_" and "_A21"

can you help me?

thanks
Posted
Comments
Thomas Daniels 30-Aug-14 10:35am    
Do you always want to get the 4th part of the string, whatever the string is?
[no name] 30-Aug-14 10:39am    
Okay and what is the trouble?

There are quite a few ways to do that, the simplest is probably to use Split:
C#
string inp = "STR_LOCAL_ABS_CERAMIUM_A21";
string[] parts = inp.Split('_');

parts[3] will have "CERAMIUM" - if the "bit" you want is always in the same potion, that will do it.
If it's always in the same position relative to the end, then use parts[parts.Length - 2] instead.
 
Share this answer
 
Comments
EADever 30-Aug-14 12:52pm    
Hi adn thanks for your help!
your solution is work for me, but it just work if string data need to get is CERAMIUM.

If i have string data input like this: STR_LOCAL_ABS_CERAMIUM_NUM_A21
and i want get output = CERAMIUM_NUM.

this question for both string input data: STR_LOCAL_ABS_CERAMIUM_A21, and STR_LOCAL_ABS_CERAMIUM_NUM_A21.

i want to get CERAMIUM and CERAMIUM_NUM
The following code could be helpful for you.

C#
using System;

namespace TestApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string strText = "STR_LOCAL_ABS_CERAMIUM_A21";

            string[] container = strText.Split('_');

            //First way
            for (int i = 0; i < container.Length; i++)
            {
                if (i == (container.Length-2))
                {
                    Console.WriteLine("value is _{0}", container[i]);
                }
            }
            //Second Way
            foreach (string item in container)
            {
                if (item.Contains("CERAMIUM"))
                {
                    Console.WriteLine("value is _{0}", item);
                }
            }
                Console.ReadKey();
        }
    }
}
 
Share this answer
 
Comments
EADever 30-Aug-14 12:55pm    
Hi adn thanks for your help!
your solution is work for me, but it just work if string data need to get is CERAMIUM.

If i have string data input like this: STR_LOCAL_ABS_CERAMIUM_NUM_A21
and i want get output = CERAMIUM_NUM.

this question for both string input data: STR_LOCAL_ABS_CERAMIUM_A21, and STR_LOCAL_ABS_CERAMIUM_NUM_A21.

i want to get CERAMIUM and CERAMIUM_NUM

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900