reverse a stringstr as string

Split string at specified delimiter
SyntaxC = strsplit(str) C = strsplit(str,delimiter) C = strsplit(str,delimiter,Name,Value) [C,matches]
= strsplit(___) Description
= strsplit() splits
the string, str, at whitespace into the cell array
of strings, C. A whitespace character is equivalent
to any sequence in the set {' ','\f','\n','\r','\t','\v'}.
= strsplit(,) splits str at
the delimiters specified by delimiter.
= strsplit(,,) specifies
additional delimiter options using one or more name-value pair arguments.
= strsplit(___) additionally returns a cell
array of strings, matches, using any of the input
arguments in the previous syntaxes. matches contains
all occurrences of delimiters upon which strsplit splits str.
Examplesstr = 'The rain in Spain.';
C = strsplit(str)C =
'Spain.'C is a cell array containing 4 strings.Split a string of comma-separated values.data = '1.21, 1.985, 1.955, 2.015, 1.885';
C = strsplit(data,', ')C =
Split a string of values, data, which
contains the units m/s with an arbitrary number
of white-space on either side of the text. The regular expression, \s*,
matches any white-space character appearing zero or more times.data = '1.21m/s1.985m/s 1.955 m/s2.015 m/s 1.885m/s';
[C,matches] = strsplit(data,'\s*m/s\s*',...
'DelimiterType','RegularExpression')C =
'm/s'In this case, the last string in C is empty.
This empty string is the string that follows the last matched delimiter.myPath = 'C:\work\matlab';
C = strsplit(myPath,'\')C =
'matlab'Split a string on ' ' and 'ain',
treating multiple delimiters as one. Specify multiple delimiters in
a cell array of strings.str = 'The rain in Spain stays mainly in the plain.';
[C,matches] = strsplit(str,{' ','ain'},'CollapseDelimiters',true)C =
'ain'Split the same string on whitespace and on 'ain',
using regular expressions and treating multiple delimiters separately.[C,matches] = strsplit(str,{'\s','ain'},'CollapseDelimiters',...
false, 'DelimiterType','RegularExpression')C =
'ain'In this case, strsplit treats the two delimiters
separately, so empty strings appear in output C between
the consecutively matched delimiters.
Split text on the strings ', ' and ',
str = 'bacon, lettuce, and tomato';
[C,matches] = strsplit(str,{', ',', and '})C =
'and tomato'
', 'Because the command lists ', ' first and ',
and ' contains ', ', the strsplit function
splits str on the first delimiter and never proceeds
to the second delimiter. If you reverse the order of delimiters, ', and
' takes priority.str = 'bacon, lettuce, and tomato';
[C,matches] = strsplit(str,{', and ',', '})C =
', and 'Input Argumentsstring
Input text, specified as a string.
Data Types: charstring | 1-by-n cell array of strings
Delimiting characters, specified as a single string or a 1-by-n
cell array of strings. Strings specified in delimiter do
not appear in the string fragments of the output .
Specify multiple delimiters in a cell array of strings. Each
element of the cell array must contain a single string in a single
row. The strsplit function splits
the elements of delimiter in the order in which
they appear in the cell array.
delimiter can include the following escape
sequences:
\\Backslash
\bBackspace
\fForm feed
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab
Example: ','Example: {'-',','}
Data Types: char | cellName-Value Pair ArgumentsSpecify optional comma-separated pairs of Name,Value arguments.
Name is the argument
name and Value is the corresponding
value. Name must appear
inside single quotes (' ').
You can specify several name and value pair
arguments in any order as Name1,Value1,...,NameN,ValueN.Example: 'DelimiterType','RegularExpression' instructs strsplit to
treat delimiter as a regular expression.1 (true) (default) | 0 (false)
Multiple delimiter handling, specified as the comma-separated
pair consisting of 'CollapseDelimiters' and either true or false.
If true, then consecutive delimiters in str are
treated as one. If false, then consecutive delimiters
are treated as separate delimiters, resulting in empty string '' elements
between matched delimiters.
Example: 'CollapseDelimiters',true
'Simple' (default) | 'RegularExpression'
Delimiter type, specified as the comma-separated pair consisting
of 'DelimiterType' and one of the following strings.
'Simple'Except for escape sequences, strsplit treats delimiter as
a literal string.
'RegularExpression'strsplit treats delimiter as
a regular expression.
In both cases, delimiter can include escape
sequences.
Output Argumentscell array of strings
Parts of the original string, returned as a cell array of strings. C always
contains one more element than
Consequently, if the original string, str, ends
with a delimiter, then the last cell in C contains
an empty string.
cell array of strings
Identified delimiters, returned as a cell array of strings. matches always
contains one fewer element than output
More About
See Also |
欢迎转载,转载请注明来自:
与matlab strsplit用法相关的文章
版权归所有|
友链等可联系 |Oracle reverse string之种种实现
Oracle reverse string之种种实现
发布时间: 17:25:32
编辑:www.fx114.net
本篇文章主要介绍了"Oracle reverse string之种种实现",主要涉及到Oracle reverse string之种种实现方面的内容,对于Oracle reverse string之种种实现感兴趣的同学可以参考一下。
转载自:http://blog.chinaunix.net/uid-7655508-id-4011549.html&
&Oracle SQL里有undocument function实现reverse string的功能,那么能否用其他方式实现呢?这里介绍几种方法:
1.undocument REVERSE FUNCTION
SELECT REVERSE('测试reverse') FROM&
--注意中文可能乱码
SELECT REVERSE('测试reverse') FROM
--2次reverse正常
SELECT reverse(REVERSE('测试reverse')) FROM
2.使用utl_raw.REVERSE
SELECT utl_raw.cast_to_varchar2(utl_raw.reverse(utl_raw.cast_to_raw('测试reverse'))) FROM
--中文同上
3.递归SQL实现reverse,好处,中文的可以直接reverse
--带分隔符,选不存在字符串的特殊符号
& (SELECT &'测试reverse' AS str FROM dual)
& SELECT REPLACE(sys_connect_by_path(res_str, '|'), '|') AS reversed_string
& FROM (SELECT length(str) - rownum AS rn, substr(str, rownum, 1) res_str
& & & & &FROM t
& & & & &CONNECT BY rownum &= length(str))
& WHERE connect_by_isleaf = 1
& CONNECT BY rn = PRIOR rn + 1
& START WITH rn = 0;
&--11g r2递归with&
&WITH t (str, s, c)&
& SELECT '测试reverse' str, CAST(NULL AS VARCHAR2(15)) s, 0
& FROM dual
& UNION ALL
& SELECT str, s || substr(str, -c - 1, 1), c + 1 FROM t WHERE c &= length(str)
SELECT MAX(str) str, MAX(s) rev_str FROM
4.使用undocument wm_concat OR 11g r2 listagg,好处,中文的可以直接reverse
SELECT REPLACE(WM_CONCAT(NAME), ',', '') REV_NAME
FROM (SELECT LEVEL, SUBSTR('测试reverse', LEVEL, 1) NAME
& & & &FROM DUAL
& & & &CONNECT BY LEVEL &= LENGTH('测试reverse')
& & & &ORDER BY 1 DESC);
SELECT listagg(str) within
ORDER BY ord)
FROM (SELECT rownum ord, substr('测试reverse', LEVEL * -1, 1) str
& & & &FROM dual
& & & &CONNECT BY LEVEL &= length('测试reverse'));
当然,undocument的东西最好别用,这里11g推荐使用listagg,或者非中文的用UTL_RAW.REVERSE实现。
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。
本文标题:
本页链接:Reverse a String
& Reverse a String
Routine Summary
Reverses the characters in a string.
Str1 - The string you want to reverse
Str1 - The reversed string
Variables Used
Str1, I, Ans
Calculator Compatibility
TI-83/84/+/SE
:For(I,1,length(Ans)-1
:sub(Ans,2I,1)+Ans
:sub(Ans,1,I→Str1
With our string stored in Str1 and , we loop through each character, starting from the beginning to the end, and add it to the beginning of the string, building the reversed string up at the beginning as we go:
(original string - the first character is the first reversed character)
(add then second character before the first, reversing the string)
(continue adding characters in reverse)
(this is what the end result looks like)
Since adding to the beginning of the string alters the indices, we must take that into account — this is where the 2I in the formula comes from. It adds the 2nd character the first time, the 4th character (which was originally the 3rd) next, the 6th character (originally the 4th) after that, and so on.
Using Ans allows us to not have to use another string variable, since Ans can act like a string and it gets updated accordingly, and Ans is also faster than a string variable.
By the time we are done with the
loop, all of our characters are put together in Ans in reverse order, before the original string. To finish, we take the first (reversed) half as a
and store it back in Str1 for further use. We can use I for this purpose because it looped from 1 to length(Str1)-1, so its value will be length(Str1) when exiting.
If you want to preserve the original string, store it to a different string variable in the first line of the code.
When you are done using Str1, you should
at the end of your program.
Powered by
Unless otherwise stated, the content of this page is licensed under .
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible).
Watch headings for an &edit& link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
See pages that link to and include this page.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).
Notify administrators if there is objectionable content in this page.
Something does not work as expected? Find out what you can do.
documentation and help section.
Terms of Service - what you can, what you should not etc.
Privacy Policy.Leetcode - Reverse Integer - 简书
Leetcode - Reverse Integer
Question:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of
overflows. How should you handle such cases?For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
public class Solution {
public int reverse(int x) {
boolean isPositive =
if (x == 0)
else if (x == Integer.MIN_VALUE)
else if (x & 0)
isPositive =
int temp = Math.abs(x);
String tempStr = Integer.toString(temp);
String reverseStr = reverseStr(tempStr);
if (!isPositive)
reverseStr = '-' + reverseS
if (Long.parseLong(reverseStr) & - || Long.parseLong(reverseStr) & )
return Integer.parseInt(reverseStr);
private String reverseStr(String tempStr) {
boolean isRightZero =
String reverseStr = "";
for (int i = tempStr.length() - 1; i &= 0; i--) {
if (tempStr.charAt(i) != '0' && isRightZero)
isRightZero =
if (!isRightZero)
reverseStr += tempStr.charAt(i);
return reverseS
public static void main(String[] args) {
Solution test = new Solution();
System.out.println(test.reverse(-));
My test result:
Paste_Image.png
这次作业比较简单,其实就是反转字符串。但是要先判断下正负情况。总结:然后仔细研究了下 Math.abs(int a) 这个方法。他有一个特殊情况。当
a = Integer.Min_VAL 时,此时如果返回绝对值,会造成溢出。所以这个方法规定仍旧返回原值。所以需要提前将这个情况用if语句考虑到。然后就基本没什么问题了。
Anyway, Good luck, Richardo!
public class Solution {
public int reverse(int x) {
int sign = (x &= 0 ? 1 : -1);
long num = Math.abs((long) x);
long temp = 0;
while (num & 0) {
long digit = num % 10;
temp = 10 * temp +
num = num / 10;
if (sign == 1) {
if (temp & Integer.MAX_VALUE) {
return (int)
if (-temp & Integer.MIN_VALUE) {
return (int) -
感觉和 实现两个数相除,还有两个数相除算循环小数的题目,差不多。就是进来先判断sign,然后转换成long,再做后面的事。
Anyway, Good luck, Richardo!
09/23/2016Reverse String
Writeafunctionthattakesastringasinputandreturnsthestringreversed. Example: Givens=hello,returnolleh. 无 package com.test1;public class ReverseString {public static void main(String[] args) {// TODO Auto-generated method stubString str="hell
Write&a&function&that&takes&a&string&as&input&and&returns&the&string&reversed.Example:Given&s&=&&hello&,&return&&olleh&.
package com.test1;
public class ReverseString {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="hello#$%^^ world";
Solution test=new Solution();
test.reverseString(str);
class Solution {
public String reverseString(String s) {
char array[]=s.toCharArray();
System.out.println(array);
// char array2[];
String str2="";
for(int i=array.length-1;i&=0;i--)
str2=str2+array[i];
System.out.println(str2);
return str2;
//class Solution {
public String reverseString(String s) {
char array[]=s.toCharArray();
System.out.println(array);
// char array2[];
String str2="";
for(int i=0,j=array.length-1;i&j;i++,j--)
temp=array[i];
array[i]=array[j];
str2=array.toString();
System.out.println(str2);
return str2;
你最喜欢的

我要回帖

更多关于 lua string.reverse 的文章

 

随机推荐