Split text by symbol: Difference between revisions

Jump to navigation Jump to search
m
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
Split text by symbol (another string, also called delimiter or separator)
Split text by symbol (another string, also called delimiter or separator)
== Example data ==
Task: Split the position e.g. 25.040215, 121.512532 by the comma ( , ) to obtain the latitude (1st string) and longitude (2nd string).
Expected output:
* 25.040215
* 121.512532


== Approaches ==
== Approaches ==
Line 7: Line 14:
* PHP: [http://php.net/explode explode] or [http://php.net/manual/en/function.preg-split.php preg_split] function
* PHP: [http://php.net/explode explode] or [http://php.net/manual/en/function.preg-split.php preg_split] function


== Example ==
Task: Split the position e.g. 25.040215, 121.512532 by the comma ( , ) to obtain the latitude (1st string) and longitude (2nd string).


Expected output:
=== Split text by symbol in MySQL ===
* 25.040215
* 121.512532
 
=== MySQL Approach ===
{{exclaim}} Only one symbol was allowed. The following query will get the last element if there are more than two symbols.
{{exclaim}} Only one symbol was allowed. The following query will get the last element if there are more than two symbols.
# to get the first string ( 25.040215 ): {{code | code=SELECT TRIM(SUBSTRING_INDEX('25.040215, 121.512532' ,',', <span style="color:red">1</span>))}}
# to get the first string ( 25.040215 ): {{code | code=SELECT TRIM(SUBSTRING_INDEX('25.040215, 121.512532' ,',', <span style="color:red">1</span>))}}
Line 28: Line 29:
</pre>
</pre>


=== Excel Approach ===
=== Split text by symbol in Excel ===
==== 1: parse data ====
Comparison of approaches
<table border="1" class="wikitable sortable">
  <tr>
    <th>Approach</th>
    <th>Output</th>   
  </tr>
  <tr>
    <td>Approach 1: Parse data</td>
    <td>'''Multiple''' parts will be returned if the content contains two or more separator characters.</td>   
  </tr>
  <tr>
    <td>Approach 2: SUBSTRING_INDEX</td>
    <td>Only '''two''' parts will be returned if the content contains two or more separator characters.</td>   
  </tr>
</table>
 
==== Approach 1: Parse data ====
[https://support.microsoft.com/en-us/kb/214261 parse data] ([https://support.microsoft.com/zh-tw/kb/214261 資料剖析])
[https://support.microsoft.com/en-us/kb/214261 parse data] ([https://support.microsoft.com/zh-tw/kb/214261 資料剖析])
* instruction with images: [https://support.office.com/en-za/Article/split-text-into-different-cells-30b14928-5550-41f5-97ca-7a3e9c363ed7 Split text into different cells - Excel]
* instruction with screenshots: [https://support.office.com/en-za/Article/split-text-into-different-cells-30b14928-5550-41f5-97ca-7a3e9c363ed7 Split text into different cells - Excel]


==== 2: SUBSTRING_INDEX ====
==== Approach 2: SUBSTRING_INDEX ====
<table border="1" style="">
<table border="1" style="">
<tr style="background-color: #555555; color: #ffffff;">
<tr style="background-color: #555555; color: #ffffff;">
Line 49: Line 66:
</table>
</table>
If the data set is perfect
If the data set is perfect
# separator character: ,
# to get the first string ( 25.040215 ): B1 = {{code | code=<span style="background-color: #F6CEF5">MID( A1, 1, FIND(",", A1)-1)</span> }}
# to get the first string ( 25.040215 ): B1 = {{code | code=<span style="background-color: #F6CEF5">MID( A1, 1, FIND(",", A1)-1)</span> }}
# to get the second string ( 121.512532 ): C1 = {{code | code=<span style="background-color: #ccc">MID( A1, FIND(",", A1)+1, LEN(A1))</span> }}
# to get the second string ( 121.512532 ): C1 = {{code | code=<span style="background-color: #ccc">MID( A1, FIND(",", A1)+1, LEN(A1))</span> }}
Line 58: Line 76:


[https://docs.google.com/spreadsheets/d/11VeZIxGR7orL4smf_0G6oi7VxxNi3FFsCL9b6QrLhBE/edit?usp=sharingc  online demo] (allow to edit)
[https://docs.google.com/spreadsheets/d/11VeZIxGR7orL4smf_0G6oi7VxxNi3FFsCL9b6QrLhBE/edit?usp=sharingc  online demo] (allow to edit)
<div style="float:left; padding-right:5px;">
{{#widget:Iframe
|url=https://docs.google.com/spreadsheet/ccc?key=11VeZIxGR7orL4smf_0G6oi7VxxNi3FFsCL9b6QrLhBE
|width=900
|height=500
|border=1
}}


=== PHP Approach ===
=== Split text by symbol in PHP ===
PHP script
PHP script
<pre>
<pre>
Anonymous user

Navigation menu