Jump to content

string split c#

Go to solution Solved by ladybug,
19 minutes ago, minibois said:

Can you post your code and show the output?

It should probably go like this:




string s = "text1=>text2=>text3=>";
string[] subs = s.Split('=', '>');

with subs now being a string array with these 'text1', etc. strings in it.

 

27 minutes ago, AbydosOne said:

Hacky answer: split on '=' and remove '>' from all resulting strings (or vice versa).

 

EDIT: potentially less hacky: Use a String ("xxx") instead of Character ('x') and see if that works.

So I tried both and it works only after adding this System.StringSplitOptions.RemoveEmptyEntries in the method for string split. I don't know what it does but it works fine so thanks

hey just a quick question can you use => as string splitter? For some reason in my task it says that the input has to be done like this. You know string.split('=>') it counts it as two symbols is there a button on the keyboard that types this symbol as one symbol? I also tried doing this string.split('=','>') but then when I type both => as a string splitter it breaks for example input=>input and so on

 

Link to comment
https://linustechtips.com/topic/1331679-string-split-c/
Share on other sites

Link to post
Share on other sites

23 minutes ago, stefanmz said:

hey just a quick question can you use => as string splitter? For some reason in my task it says that the input has to be done like this. You know string.split('=>') it counts it as two symbols is there a button on the keyboard that types this symbol as one symbol? I also tried doing this string.split('=','>') but then when I type both => as a string splitter it breaks for example input=>input and so on

 

Hacky answer: split on '=' and remove '>' from all resulting strings (or vice versa).

 

EDIT: potentially less hacky: Use a String ("xxx") instead of Character ('x') and see if that works.

Main System (Byarlant): Ryzen 9 5950X | Asus B550-Creator ProArt | EK 240mm Basic AIO | 32GB G.Skill DDR4 3600MT/s CL16 | XFX Speedster SWFT 210 RX 6600 | Samsung 990 PRO 2TB / Samsung 990 EVO Plus 4TB | Corsair RM750X | StarTech 4× USB 3.0 Card | Realtek RTL8127 10G NIC | Hyte Y60 Case | Dell U3415W Monitor | Keychron K12 Blue (RGB backlight)

 

Laptop (Narrative): Lenovo Flex 5 81X20005US | Ryzen 5 4500U | 16GB DDR4 3200MT/s (soldered) | Vega II 384SP Graphics | SKHynix P31 1TB NVMe SSD | Intel AX200 Wifi | Asus 2.5G USB NIC | Asus ProArt PA278QV | Keychron K4 Brown (white backlight)

 

Proxmox Server (Veda): Ryzen 7 3800XT | ASRock Rack X470D4U | Corsair H80i v2 | 128GB Micron DDR4 ECC 3200MT/s | 2× Samsung PM963a 960GB SSD / 4× WD 10TB / 4× Seagate 14TB Exos / 4× Micron MX500 2TB / 8× WD 12TB (custom external SAS enclosure) | Seasonic Prime Fanless 500W | Intel X550-T2 10G NIC | LSI 9300-8i HBA | Adaptec 82885T SAS Expander | Fractal Design Node 804 Case

 

Proxmox Server (La Vie en Rose)GMKtec Mini PC | Ryzen 7 5700U | 32GB Lexar DDR4 (SODIMM) | Vega II 512SP Graphics | Lexar 1TB 610 Pro SSD | 2× Realtek 8125 2.5G NICs


Media Center/Video Capture (Jesta Cannon): Ryzen 5 1600X | ASRock B450M Pro4 R2.0 | Noctua NH-L12S | 16GB Crucial DDR4 3200MT/s | EVGA GTX750Ti SC | UMIS NVMe SSD 256GB / TEAMGROUP MS30 1TB | Corsair CX450M | Viewcast Osprey 260e Video Capture | TrendNet (AQC107) 10G NIC | LG WH14NS40 BD-ROM | Silverstone Sugo SG-11 Case | Sony XR65A80K

 

Workbench (Doven Wolf): Lenovo m715q | Ryzen Pro 3 2200GE | 16GB Crucial DDR4 3200MT/s (SODIMM) | Vega 8 Graphics | SKHynix (OEM) 256GB NVMe SSD | uni 2.5G USB NIC | HDMI add-in module

 

Network:

Spoiler
                       ┌─────────────── Office/Rack ───────────────────────────────────────────────┐
Google Fiber Webpass ── Cloud Gateway Max ══╦═ Pro XG 8 ══╦═ Flex 2.5-8 ══╦═ Doven Wolf
                      La Vie en Rose (DNS) ═╬═ Narrative  ╠═ Veda-NAS     ╠═ La Vie en Rose (vmbr)
                                Veda (DNS) ─┘             ╠═ Veda (vmbr)  ├─ Ptolemy (vmbr)
╔═════════════════════════════════════════════════════════╩═ Ptolemy-NAS  ├─ Veda (Mgmt)
║   ┌ Closet ┐      ┌───────── Bedroom ─────────┐                         └─ Veda (IPMI)
╚═══ Flex XG ══╦╤═══ Flex XG ══╤╦═ Byarlant
       (PoE)   ║│              │╠═ Narrative 
Kitchen Jack ══╣└─ Dual PoE ┐  │╚═ Jesta Cannon*
   (Testing)   ║┌─ Injector ┘  └── Work Laptop
     Bedroom ══╝│        ┌─────── Media Center ────────────────────────────┐
     Jack #2    └──────── Switch 8 ────────────┬─ nanoHD Access Point (PoE)
Notes:                                         ├─ Sony PlayStation 4 
─── is Gigabit / ═══ is Multi-Gigabit          ├─ Pioneer VSX-S520
* = cable passed from Bedroom to Media Center  └─ Sony XR65A80K (Google TV)
Link to comment
https://linustechtips.com/topic/1331679-string-split-c/#findComment-14685512
Share on other sites

Link to post
Share on other sites

I think this is what you are looking for: https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=net-5.0#System_String_Split_System_Char___

 

So if I am understanding you correctly, you have a string like this:

text1=>text2=>text3=>

And you need to extract text1, text2 and text3?

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/1331679-string-split-c/#findComment-14685518
Share on other sites

Link to post
Share on other sites

No I was acttually wondering

1 minute ago, minibois said:

I think this is what you are looking for: https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=net-5.0#System_String_Split_System_Char___

 

So if I am understanding you correctly, you have a string like this:


text1=>text2=>text3=>

And you need to extract text1, text2 and text3?

yes but I checked this page it doesn't work if I put them as tow symbols = and >

Link to comment
https://linustechtips.com/topic/1331679-string-split-c/#findComment-14685522
Share on other sites

Link to post
Share on other sites

Just now, stefanmz said:

No I was acttually wondering

yes but I checked this page it doesn't work if I put them as tow symbols = and >

Can you post your code and show the output?

It should probably go like this:

string s = "text1=>text2=>text3=>";
string[] subs = s.Split('=', '>');

with subs now being a string array with these 'text1', etc. strings in it.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/1331679-string-split-c/#findComment-14685535
Share on other sites

Link to post
Share on other sites

19 minutes ago, minibois said:

Can you post your code and show the output?

It should probably go like this:




string s = "text1=>text2=>text3=>";
string[] subs = s.Split('=', '>');

with subs now being a string array with these 'text1', etc. strings in it.

 

27 minutes ago, AbydosOne said:

Hacky answer: split on '=' and remove '>' from all resulting strings (or vice versa).

 

EDIT: potentially less hacky: Use a String ("xxx") instead of Character ('x') and see if that works.

So I tried both and it works only after adding this System.StringSplitOptions.RemoveEmptyEntries in the method for string split. I don't know what it does but it works fine so thanks

Link to comment
https://linustechtips.com/topic/1331679-string-split-c/#findComment-14685548
Share on other sites

Link to post
Share on other sites

2 hours ago, stefanmz said:

 

So I tried both and it works only after adding this System.StringSplitOptions.RemoveEmptyEntries in the method for string split. I don't know what it does but it works fine so thanks

What's happening if you don't do that is that it's treating the 0-character gap in between the = > as a string:

 

a=>b
'a', '=', '', '>', 'b'

 

so you get a 3-element array instead of 2.

Link to comment
https://linustechtips.com/topic/1331679-string-split-c/#findComment-14685966
Share on other sites

Link to post
Share on other sites

13 hours ago, stefanmz said:

hey just a quick question can you use => as string splitter?

Yes. Read the documentation for string.Split(). Two overloads of this method take a string[] as an argument.

 

var text = "Hello=>here's=>some=>text=>and some symbols: 2=2, 5>4";
var delimiters = new string[] { "=>" };

string[] splittedStrings = text.Split(delimiters, StringSplitOptions.None);

foreach (var part in splittedStrings) Console.WriteLine(part);

// Output:
// Hello
// here's
// some
// text
// and some symbols: 2=2, 5>4

 

 

The suggestions above to split on chars '=' and '>' are not acceptable, because, well, the task is to split on the string "=>", not on chars '=' and '>'.

var text = "Hello=>here's=>some=>text=>and some symbols: 2=2, 5>4";
var separators = new char[] { '=', '>' };

string[] splittedStrings = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);

foreach (var part in splittedStrings) Console.WriteLine(part);

// Output:
// Hello
// here's
// some
// text
// and some symbols: 2
// 2, 5
// 4

 

Link to comment
https://linustechtips.com/topic/1331679-string-split-c/#findComment-14686811
Share on other sites

Link to post
Share on other sites

On 4/27/2021 at 3:47 AM, Grabhanem said:

What's happening if you don't do that is that it's treating the 0-character gap in between the = > as a string:

 

a=>b
'a', '=', '', '>', 'b'

 

so you get a 3-element array instead of 2.

Oh I understand thanks for the info!

Link to comment
https://linustechtips.com/topic/1331679-string-split-c/#findComment-14690591
Share on other sites

Link to post
Share on other sites

16 hours ago, Sakuriru said:

I'm not sure why you would want to split on => since that's the lambda operator. That's weird.

Because my task from my teacher said input to be done like this. I don't know if he insists on using this or it's just his choice and we can do whatever we want but just in case. Honestly I would have used , but I decided to do it exactly as shown in his example where it was string1=>string2 and his strings were separated by this

Link to comment
https://linustechtips.com/topic/1331679-string-split-c/#findComment-14690595
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×