> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/c-c++-for-embedded-programming/string-in-c++/split.md).

# Split

&#x20;         หลังจากการนำ string มาต่อกัน การแบ่ง string ออกจากกันก็มีความสำคัญเช่นกัน ซึ่งสามารถนำไปประยุกต์ใช้กับอุปกรณ์ไมโครคอนโทรลเลอร์ ที่รับค่ามาจากหลายๆที่และส่งออกไปเป็นข้อความข้อความเดียวแล้วจึงนำไปตัดเป็นส่วนๆเพื่อวิเคราะห์

```cpp
// C++ program to print words in a sentence
#include <iostream> 
#include <string.h> 
using namespace std;


void split(string str,string store[])
{
	string word = "";
  int a = 0;
	for (auto x : str) 
	{
		if (x == ' ') // split when see spacebar
		{
			cout << word << endl;
      store[a] = word;
      a++;
			word = "";
		}
		else {
			word = word + x;
		}
	}
  store[a] = word;
	cout << word <<"\n"<< endl;
}

// Driver code
int main()
{
	string str = "A:8 C:9 J:7 K:9";
  string store[5];
	split(str,store);
	//print store value
  /*for (int x=0; x<=5;x++){
     cout<<store[x]<<endl;
  }*/
	return 0;
}

```

![ผลการตัดข้อความด้วย spacebar](/files/-MTAPoXbbZQOFyj17714)

จากตัวอย่างด้านบน เป็นตัวอย่างการแบ่งข้อความเป็นส่วนๆเมื่อเจอ spacebar ในข้อความเราสามารถเปลี่ยนให้แบ่งข้อความด้วยอย่างอืนได้เช่นเครื่องหมาย  " : "&#x20;

```cpp

		if (x == ':') // split when see colon
		{
			cout << word << endl;
      store[a] = word;
      a++;
			word = "";
		}
		else {
			word = word + x;
		}
	
```

![ผลการตัดข้อความด้วย " : "](/files/-MTAQ9kBqjVKVynkSZXv)

{% hint style="info" %}
การระบุ format ของข้อความแบบนี้สามารถพบเห็นได้กับไฟล์ประเภท .csv, .json&#x20;
{% endhint %}

reference: [Geeksforgeeks](https://www.geeksforgeeks.org/split-a-sentence-into-words-in-cpp/)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.aic-eec.com/c-c++-for-embedded-programming/string-in-c++/split.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
