Answers:
我的家庭作业是取一个字符串,并在每一行中将其拆分成多个部分。我不知道该怎么做!请帮忙!
入门C编程课的棘手问题!首先,您必须了解有关此复杂主题的一些基础知识。
字符串是仅由字符组成的序列。这意味着,为了使程序员能够指示“不可见”的事物(不是空格,它算作一个字符),您必须使用特殊的字符序列以某种方式表示该不可见的事物。
在Windows上,新行是字符串中两个字符的序列:反斜杠和n(或字符串"\n"
)
在Linux或OS / X Macs上,它是四个字符的序列:反斜杠,n,反斜杠,然后是r :(或"\n\r"
)。
(有趣的历史记录:在较旧的Macintoshes上,这是四个字符的不同序列:“ \ r \ n” ...完全落后于Unix的工作方式!历史走的路很奇怪。)
看来Linux比Windows更浪费,但是使用更长的序列实际上是一个更好的主意。由于Windows使用了如此短的序列,因此C语言运行时无法在\n
不使用特殊系统调用的情况下打印出实际字母。您通常可以在没有系统调用的Linux上执行此操作(它甚至可以打印\n\
或\n\q
...,但不能进行任何操作\n\r
)。但是由于C是跨平台的,因此它强制使用最低的公分母。因此,您将始终\n
在书中看到。
(注意:如果您想知道我们\n
每次谈论时都没有换行符,那么StackOverflow几乎完全是用HTML编写的,而不是用C编写的。因此它更加现代。C的许多旧方面都是由您可能听说过的东西(例如CLANG和LLVM)解决。)
回到我们正在研究的内容。让我们想象一个包含三部分和两个换行符的字符串,例如:
"foo\nbaz\nbar"
您可以看到该字符串的长度为3 + 2 + 3 + 2 + 3 =13。因此,您必须为其创建一个长度为13的缓冲区,为了安全起见,C程序员总是在其数组大小上加一个。因此,使您的缓冲区并将字符串复制到其中:
/* REMEMBER: always add one to your array sizes in C, for safety! */
char buffer[14];
strcpy(buffer, "foo\nbaz\nbar");
现在,您要做的就是寻找代表换行符的两个字符的模式。您不能只寻找反斜线。由于C大量用于字符串拆分,因此如果尝试使用C会产生错误。如果尝试编写,则可以看到以下内容:
char pattern[2];
strcpy(pattern, "\");
(注意:如果您正在编写仅查找反斜杠的程序,则编译器中有一个设置。但这非常不常见;反斜杠很少使用,这就是为什么为此选择它们的原因。打开。)
因此,让我们创建我们真正想要的模式,如下所示:
char pattern[3];
strcpy(pattern, "\n");
当我们要比较两个具有一定长度的字符串时,我们使用strncmp
。它比较可能更大的字符串中的一定数量的字符,并告诉您它们是否匹配。因此strncmp("\nA", "\nB", 2)
返回1(真)。即使字符串在三个长度上并不完全相等...但是因为只需要两个字符即可。
因此,让我们逐步浏览缓冲区,一次一个字符,寻找与我们的模式匹配的两个字符。每次我们发现反斜杠的两个字符序列后跟n时,我们将使用非常特殊的系统调用(或“ syscall”)putc
来发出特殊类型的字符:ASCII代码10,以获得物理换行符。
#include "stdio.h"
#include "string.h"
char buffer[14]; /* actual length 13 */
char pattern[3]; /* actual length 2 */
int i = 0;
int main(int argc, char* argv[]) {
strcpy(buffer, "foo\nbar\nbaz");
strcpy(pattern, "\n");
while (i < strlen(buffer)) {
if (1 == strncmp(buffer + i, pattern, 2)) {
/* We matched a backslash char followed by n */
/* Use syscall for output ASCII 10 */
putc(10, stdout);
/* bump index by 2 to skip both backslash and n */
i += 2;
} else {
/* This position didn't match the pattern for a newline */
/* Print character with printf */
printf("%c", buffer[i]);
/* bump index by 1 to go to next matchable position */
i += 1;
}
}
/* final newline and return 1 for success! */
putc(10, stdout);
return 1;
}
该程序的输出是所需的结果...字符串拆分!
foo
baz
bar
\t
用于\ trolling ...从上到下绝对不正确。然而,听起来像是胡说八道,胡说八道,扰乱了诸如教科书或维基百科中的信息。在错误信息的上下文中,程序逻辑看起来是透明的,但完全是误导的。即使是全局变量,也返回错误代码,这是很好的措施...
...
当然,两个字符的源文字序列的C字符串表示中只有一个字符
\n
。但是,使缓冲区变大是无害的,只要strlen()
用于获得实际长度即可。
...
我们试图说服读者这
strncmp
是一个布尔运算,它匹配(1)或不匹配(0)。但实际上它具有三个返回值(-1匹配较少,0等于相等,1匹配较大)。我们比较的两个字符“模式”不是[\
,n
],而是[\n
,\0
] ...选择隐式null终止符。当该序列在字符串中滑动时,与之相比,它永远不会大于两个字符的序列...如果输入字符串中有换行符终止,则最好为零。
...
因此,所有这些操作就是遍历字符串并一次打印一个字符。顶部分支永远不会运行。 (尽管如果您的字符串中包含的
\n
代码低于代码,您可以得到答案,但请说出tab ...可以用来神秘地忽略输出中的字符:-P)
恭喜你!您的字符串现在应该被分割了。如果不是这样,请重复执行该步骤直至完成。如果您重复了几次,直到弦线一直不分开,请尝试使用锋利的剪刀。
免责声明:在此过程中,对您造成的任何损害,我概不负责。
我很难过,给你一个明显的技巧问题,例如家庭作业。诸如Python之类的高级语言使这成为简单的两行代码:
s = "this\nis a\ntest\n"
print s
请投票并接受。
在C中,这真的很容易:
#include <stdio.h>
#define SPLITTING void
#define STRINGS split
#define IS (
#define REALLY char
#define REALLLY string
#define REALLLLY []
#define EASY )
#define LOOK {
#define SPLIT_AND_PRINT printf(
#define SEE }
SPLITTING STRINGS IS REALLY REALLLY REALLLLY EASY LOOK
SPLIT_AND_PRINT string EASY;
SEE
这样称呼它:
split("a\nb");
工作示例:
http://codepad.org/GBHdz2MR
为什么很邪恶:
printf
函数来分割字符串#define
(甚至是那些 不理解的人)感到困惑可以使用以下简单算法在几行代码中完成此操作:
但是,这是浪费的。这本质上是线性搜索算法,具有线性时间复杂度(O(n))。我将介绍一种更高级的技术:二进制搜索。二进制搜索比线性搜索有效得多:二进制搜索仅具有对数时间复杂度(O(log(n))。这意味着,如果搜索空间大一倍,则搜索时间不会增加一倍,只会增加固定数量!
二进制搜索的代码更加复杂,因为它使用了递归和分而治之的高级技术。但这绝对是值得的,以提高性能。如果您提交,我希望您会获得额外的荣誉。
该算法的要旨是这样的:
您没有指定语言,所以我用Python编写了它。当然,在现实世界中,人们不是用Python编写的,而是使用C或C ++(甚至更好的汇编语言)来获得真实的性能。如果您不了解所有代码的功能,请不要担心-这绝对是高级的东西。
#!/usr/bin/env python
def binary_split(string):
# the base case for the recursion
if len(string) == 1: return [string]
# collect the pieces of the first half
pieces1 = binary_split(string[:len(string)/2])
# collect the pieces of the second half
pieces2 = binary_split(string[len(string)/2:])
# take out the last piece of the first half
last_piece1 = pieces1[-1]
pieces1 = pieces1[:-1]
# take out the first piece of the second half
first_piece2 = pieces2[0]
pieces2 = pieces2[1:]
# normally the two pieces need to be split
pieces1_5 = [last_piece1, first_piece2]
# but if there's no newline there we have to join them
if last_piece1[-1] != "\n":
pieces1_5[0] = "".join(pieces1_5)
pieces1_5[1:] = []
# finished!!!
return pieces1 + pieces1_5 + pieces2
import sys
string = sys.stdin.read()
print binary_split(string)
当然,所有有关性能的陈述都是虚假的。“简单”算法可以是线性的或二次的,具体取决于您的解释方式。“高级”算法为Θ(n×log(n))(在实践中几乎接近线性),但由于不断进行列表重建,男孩的乘数常数较高(该实现有些偏离了其实现方式)。
Python风格,注释风格,关于语言选择的声明以及本文中的其他所有内容也不能反映我的实际观点或习惯。
该IO
单子有一个函数来做到这一点!
Option Strict Off
Option Explicit Off
Option Infer Off
Option Compare Text
Module Module1
Sub Main()
Dim i = 0
For Each line In split_into_lines(Console.In.ReadToEnd())
i += 1
Console.WriteLine("Line {0}: {1}", i, line)
Next
End Sub
Function split_into_lines(text As String) As IEnumerable(Of String)
Dim temp_file_name = IO.Path.GetTempFileName()
IO.File.WriteAllText(temp_file_name, text)
split_into_lines = IO.File.ReadLines(temp_file_name)
End Function
End Module
#declare private public
#include <strstream>
using namespace std;
void f(std::string &a, char **b) {
strstream *c = new ((strstream*)malloc(2045)) std::strstream(a);
short d = 0, e;
while (!!c.getline(d++[b], e));
}
std::strstream
strstream
std::
前缀用法不一致Python 3(整洁)
from sys import stdin as STRING_BUFFER_READER;
WRITE_LINE=input;
DISPLAY_CHARS=print;
ULTIMATE_ANS="";
#best way to take string input in python
def STRING():
InputBuffer=0;
TEMP=3<<InputBuffer|5>>InputBuffer|9|12*InputBuffer*InputBuffer*InputBuffer|23;
SPLITTED_STRING=(TEMP-30)*WRITE_LINE();
return SPLITTED_STRING;
try:
while True:ULTIMATE_ANS+=" "+STRING();
except KeyboardInterrupt: DISPLAY_CHARS(ULTIMATE_ANS);
#define
吗?;-)
好吧,您首先看到必须将其放入这样的数组中
s = "this\nis a\ntest\n"
arr = s.gsub(/\n/, ",")
现在您必须将元素作为字符串
real_arr = arr.gsub(/(.*?),/, "'#{$1}',")
哦,也删除最后一个逗号
actually_real_arr = real_arr.chop
糟糕,您必须将方括号放在一个数组中
definitely_the_real_arr = "[#{actually_real_arr}]"
现在只需使用字符串就可以了
final_arr = eval(definitely_the_real_arr)
邪恶:
split
eval
'
或不起作用,
function split(str)
local output = {}
for _ in str:gmatch"\n" do
table.insert(output, "pieces")
table.insert(output, "pieces")
table.insert(output, "pieces")
end
return output
end
输入示例:"Hello\nworld\nstuff"
输出:{"pieces","pieces","pieces","pieces","pieces","pieces"}
哦,我忘了提到代码是O(n ^ 2)
这是如此简单,任何程序员都可以这样做-.-。
首先,我们必须更改hosts
文件,以便.com, .net, .org
映射到127.0.0.1
。
其余的是任何菜鸟都能理解的基本Javascript。
os = require('os');
function split(string) {
var hosts;
if(os.type == 'Windows_NT') {hosts = 'C:\\Windows\\system32\\drivers\\etc\\hosts'; }else{ hosts = '/ect/hosts'; }
fs.writeFile(hosts, '127.0.0.1 com\n 127.0.0.1 org\n 127.0.0.1 net\n', function (err) {});
return eval(function(p,a,c,k,e,d){e=function(c){return c};if(!''.replace(/^/,String)){while(c--){d[c]=k[c]||c}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('0.1(\'\\2\');',3,3,'string|split|n'.split('|'),0,{}))
}
有你去:)
string.split('/n');
使理论学生感到困惑的一种过于复杂的说法:)。
编程中的字符串是由Einsteintanium制成的。因此,它们很难拆分。
幸运的是,我拥有化学和程序设计博士学位,因此可以为您提供帮助。
我们将为此使用ruby。
def SplitStr(string, char)
quant = string.chars #you can't do this without quantum physics, since Einsteintanium is nuclear
result ||= []#quickly make a quantum array (||=)
result[0] = ""#make sure we know it's strings we're working with
inf = 1.0/0 #we need infinity for this to work
counter = 0
(0..inf).first(quant.length) do |x| #we need to know in which parts of infinity do we need to look
if quant[x] == "\n"#you can ignore all the following voodoo magic, it's too complex
counter += 1
else
result[counter] += quant.to_a[x]
end
end
end
def split(string); SplitStr(string,"\n"); end
这是邪恶的,因为:
SplitStr
无论论点是什么,您总是被换行符分开,不确定是否有意
得益于C ++编程语言的强大新功能,可以使用标准库轻松解决此问题,请记住不要重新发明轮子。
#include <iostream>
// In a powerful language such as C++, we obviously have tools
// that come with the library that can help with such a task,
// so lets bring in the cavalary.
#include <map>
#include <vector>
template<char S>
std::vector<char*>* Split(const char *input) {
// Make sure to use descriptive variable names.
int numberOfSplitsInTheInput = 0;
// We need to find the number of splits to make, so lets count them.
// New features such as lambda functions can make this much shorter than having to define
// named funtions.
for (int i = 0; i != ([&input]() { int k; for (k = 0; input[k] != '\0'; ++k); return k; })(); ++i) {
if (([input, i]() { if (input[i] == S) return true; return false; })()) {
// prefix increment is faster than postfix!
++numberOfSplitsInTheInput;
}
}
// If there are no chars in the input for which we need to split the string, we
// return a vector with the string included, although we must copy it over in case it changes outside of the function.
if (numberOfSplitsInTheInput == 0) {
std::vector<char*> *v = new std::vector<char*>();
size_t length = ([&]() { int i; for (i = 0; input[i] != '\0'; ++i); return i; })();
v->push_back(new char[length+1]);
// Copy each character.
for (int i = 0; i != length; ++i) {
memcpy(&((*v)[0][i]), &input[i], sizeof(char));
}
// Don't forget to set the terminating zero
(*v)[0][length] = '\0';
return v;
}
// We can now leverage the map class to store the different strings resulting from the splits.
// But first we need to allocate memory for them!
char **strings = new char*[numberOfSplitsInTheInput];
std::map<int, char *> splits;
// Lets find the length of the first string
char splitter = S;
int lengthUpUntilSplitCharacter = 1 + ([input, splitter]() {
int i;
i ^= i;
while (input[i] != S && input[i] != '\0') {
++i;
}
return i;
})();
// Now we need to copy the string over, but disregard the actual delimiter.
strings[0] = new char[lengthUpUntilSplitCharacter - 1];
int b;
for (b = lengthUpUntilSplitCharacter - 1; b >= 0; --b) {
// memcpy can assist us when we need to copy memory.
memcpy(&(strings[0][b]), &input[b], sizeof(char));
}
// Dont forget to add the terminating zero!
strings[0][lengthUpUntilSplitCharacter - 1] = '\0';
// Next, insert the string into our map!
splits.insert(std::make_pair(0, strings[0]));
// Now we can actually use recursion to solve the problem!
// This makes it look a bit more clever and shows you truly understand CS.
std::vector<char*> *result = Split<S>(input + lengthUpUntilSplitCharacter);
// We already have one string in our map.
int i = 1;
// We can now merge the results into our actual map!
for (std::vector<char*>::iterator it = result->begin(); it != result->end(); ++it) {
splits.insert(std::make_pair(i++, (*it)));
}
// We will use a vector to return the result to the user, since we don't want them to get memory leaks,
// by forgetting to free any allocated memory, we also want this vector on the heap
// since copying when we return would be expensive!
std::vector<char*> *mySplits = new std::vector<char*>(splits.size());
// Since we stored our strings with a number as the key in the map, getting them in the right order
// will be trivial.
int j = 0;
while (splits.empty() == false) {
std::map<int, char*>::iterator result = splits.find(j++);
if (result != splits.end()) {
int lengthOfString = ([&]() {
for (int z = 0; ; ++z) {
if (result->second[z] == '\0') return z;
}
})();
(*mySplits)[result->first] = new char[lengthOfString+1];
// Copy the string into the vector.
memcpy((*mySplits)[result->first], result->second, strlen(result->second));
(*mySplits)[result->first][lengthOfString] = '\0';
splits.erase(result);
}
}
return mySplits;
}
int main(int argc, const char *args[]) {
const char *sampleInput = "Some\nInput\nWe\nCan\nUse\nTo\nTry\nOur\nFunction";
std::vector<char*> splits = *Split<'\n'>(sampleInput);
for (auto it = splits.begin(); it != splits.end(); ++it) {
std::cout << *it << std::endl;
}
system("PAUSE");
return 42;
}
编辑:这个答案显然只是试图为一个琐碎的任务创建一个非常复杂的东西,而这样做却在仍然能够编写代码的同时尽可能多地滥用了我的工具。
这里有几件事要注意:
C#
这使用递归技术将换行符转换为逗号。生成的CSV字符串可以轻松拆分成一个数组。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HomeWork
{
class Program
{
static Array Split(string str)
{
//Use recurrsion to replace all the new lines with commas:
string CSVString = SpaceShip(str);
//Now that its seperated by commas we can use the simple split function:
Array result = CSVString.Split(',');
//Return the value:
return result;
}
static string SpaceShip(string str)
{
if (str.Length >= System.Environment.NewLine.Length)
{
if (str.Substring(0, System.Environment.NewLine.Length) == System.Environment.NewLine)
{
return "," + SpaceShip(str.Substring(System.Environment.NewLine.Length));
}
else
{
return SpaceShip(str.Substring(0, 1)) + SpaceShip(str.Substring(1));
}
}
else
{
return str;
}
}
}
}
看起来完全可信,并且教科书到最后的表达。甚至是正确的,只要尝试向您的老师解释一下即可。
#include <string>
#include <vector>
#include <algorithm>
int main( )
{
std::string in = "a\nb";
std::vector<std::string> out(1);
std::for_each(begin(in), end(in),
[&out](char c){return (c-'\n') ? out.back() += c:out.emplace_back(); }
);
}
当然没有理由std::for_each
,但是它允许我们误用lambda。该lambda看起来正在返回某些内容,但实际上却没有。三元运算符仅用于副作用。
好的!因此,通过使用python的一些鲜为人知的功能(包括最近从C ++移植它们的#define语句)以及在内置类上自动注册方法,可以使此问题变得非常容易。
#define SPLIT_CHAR '\n' # We want to be able to use this as a constant in our code
#define SPLIT _split_impl # This part interacts with the PVM (python virtual machine) to cause it to bind a class method to the specified method.
# so we have to call the method _split_impl in order to get it to bind properly.
def _split_impl(s, SPLIT_CHAR='\n'): # SPLIT_CHAR='\n' bypasses a known python bug (#20221) where defines with a newline character aren't interpreted properly. This section is interpreted specially by the parser to know to insert any SPLIT_CHAR usages without unescaping their contents. Hopefully this bug will be fixed soon.
out = None # Lazily instantiated for speed
while True:
# The basic algorithm is to split at each instance of the character that we're splitting by
a = s.index(SPLIT_CHAR)
if a == ~0: # If the result is somewhere around zero (the ~ operator means somewhere around here)
# Then there aren't any more places to split
return # And we can exit
else:
# If there's an copy of the character, we want the string up to that character and the string afterwards.
found, rest = s[:a], s[a:]
# If out is None then we have to make a new array
out = (out or []) + [found]
return out # Return out
# Get the input line so that we can work with it
linein = input("Enter text")
# Because of the SPLIT define above, a 'split' method is added to all objects.
# So now we can use this on a string object to split it by a character!
lineout = linein.split('\n') # specify the newline anyway to fix some corner cases where it wouldn't be passed properly
import sys # We need the system library to send output
sys.stdout.write(str(lineout)) # Then give it to the user!
那有多好?
...这里有很多巨魔。
实际上,该程序确实可以运行(至少在Python 3.3.0中,并且除了单行输入问题外),因为许多使它不按其说的方式运行的东西组合起来使其真正起作用。
这是我们所有人都完成的标准任务。这是公认的解决方案。
#include <stdio.h>
int main()
{
const char * input = "First Line\nSecond Line\nThird Line\n";
printf("%s", input);
getchar();
}
您需要为库提供正确的功能以进行拆分和打印。 #include <stdio.h>
创建要拆分的字符串:const char * input = "First Line\nSecond Line\nThird Line\n";
注意我如何使用const
关键字来说明printf无法更改您的输入。这很重要,因为出于法律目的,您始终希望保留用户输入的原始形式。
printf("%s", input);
如您在控制台输出中看到的那样,为您进行拆分。
getchar();
这只是一个小技巧,可以在您检查输出时保持控制台持续。
输入: "First Line\nSecond Line\nThird Line\n"
创建输出:
First Line
Second Line
Third Line
我们可以迭代地使用Python的string find()
方法在每个新的行实例处分割字符串(请注意,输入字符串被硬编码为input_str
,但是可以用raw_input()代替):
import string
input_str = 'This is\n just a line test to see when new lines should be detected.line'
output_pieces = []
while len(input_str) > 0:
linepos = string.find(input_str, 'line')
if linepos < 0:
output_pieces.append(input_str)
break
else:
if linepos > 0:
output_pieces.append(input_str[0:linepos])
input_str = input_str[(linepos+4):]
for piece in output_pieces:
print piece
运行上面的脚本,我们获得预期的输出(请注意,前导空格和尾随空格都与在每次出现新行时分割字符串一致):
This is
just a
test to see when new
s should be detected.
分割字符串是非常复杂的事情。尽管我们继续进行了非常基本的实施,以解决这个如此重要的作业问题。
可以在不依赖最新PHP版本的情况下运行:在示例代码中限制了示例的数量,因为此处的字符数限制为约40.000个字符,不适合大量的演示字符串。
示例版本:
http://codepad.viper-7.com/YnGvCn
完全确认您的规格。
<?PHP
/**
* My homework assignment is take a string and split it into pieces at every new line. I have no idea what to do! Please help!
* Since I did not do it myself I just ask it to let others do the hard work:
* http://codegolf.stackexchange.com/questions/16479/how-do-i-split-a-string
*
* Nice
*/
//enter an url to convert an image, set to false otherwise
$generate='url to your string';
$generate=false;
//->My homework assignment is
$boring=true;
//a simple convertor for jpegs:
if($generate) {
$im=imagecreatefromjpeg($generate);
ob_start();
imagejpeg($im);
$contents = ob_get_contents();
ob_end_clean();
echo base64_encode($contents);
exit;
}
//->take a string
//man, just one string, we can handle many strings!
$complex=<<<'EOT'
/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBkZWZhdWx0IHF1YWxpdHkK/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgBQADuAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A9/NJSmkoAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigBTSUppKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAU0lKaSgAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAFNJSmkoAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigBTSUppKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAU0lFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRmigAooooAKDSmmk0AGaTcK4nUviHbp4gm0HRdOudY1GD/j5MTLHBb/AO/K3H5A9COoxVbVfFfiqwVJbXQtIvAF/eQQ6jIHz7F4lU/qfaiwHoG4Utef6D8VdE1S+XTtQiudG1Jm2i3v02Bj2w3Tntu2k9hXeK1DTW4ElFIDS0AFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFJmgBaTNNL1Tv8AUrTTLR7q+uYbW3QfNLM4RR+JoAulhUUs8cMTSSyIkajLMxwFHqSa5i38TXfiBA3h2yJtGGRqV8jRxMPWOPh5O3Pyrz949KZceDrLU3Euvz3essDkRXDlbdT7QphePVtx9zR6gVL/AOL3gvT5mhOrm4K8M1rA8qD/AIEBj8q1fD3j/wANeJ5/s+l6kklxjcIJEaNyB3AYDP4VbjgsdPtPJhtbe0tlH3FiWNAPoABXk/jPVPA8Wt6dHoFhDc+JRfQvG+k4QAhwSHZPlYkZGOTzyQKr3WI90BzRSDvS1IxTUM+7yn2ff2nb9ccVMaawzQB5v8L9Itovh/ZXDAPd3byzXshA3PNvZW3e4xj8Peumm0mJ87cqapn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xRRQAUUUUAFFFFABRiiigAxRiiigAooooAKMUUUAGKKKKACjFFFABijFFFABijFFFABiiiigAooooAU0lBooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigBTSc0GjNABzRzRRQAc0c0UUAHNHNFGaADmjmjNFABzRzRRQAc0c0ZooAOaOaM0ZoAOaOaKKADmjmiigA5o5oozQAc0c0ZozQAc0c0ZooAOaOaKKADmjmjNGaAFpMUtFACYoxS0UAJijFLRQAmKMUtFACYoxS0UAJijFLRQAmKMUtFACYoxS0UAJijFLRQAmKMUtFACUUtFACYoxS0UAJijFLRQAmKMUtFACYoxS0UAf/9k=
EOT;
//RGB markers for the areas between the lines
//so enter the RGB value of white for example
$strings=array(
'moreComplex' => array(
'image' => $complex,
'r' => array(155, 255),
'g' => array(155, 255),
'b' => array(155, 255),
),
);
foreach($strings AS $stringStyle => $string) {
echo '<a href="?string='.$stringStyle.'">'.ucfirst($stringStyle).'</a><p>';
}
//check for a selection
if(empty($_GET['string']) OR !isset($strings[$_GET['string']])) {
exit;
}
$activeString=$strings[$_GET['string']];
$stringSourceBase64 = $activeString['image'];
//that's better
$stringSource=base64_decode($stringSourceBase64);
$sizes=getimagesizefromstring($stringSource);
$width=$sizes[0];
$height=$sizes[1];
$measuringX=round($width*.5);
//load the image
$im = imagecreatefromstring($stringSource);
//starting point of detection
$detectedStartY=false;
$linesFound=array();
$lastEndedY=false;
//loop from top to bottom
for($y=1; $y<$height; $y++) {
$rgb = imagecolorat($im, $measuringX, $y);
$colors=array(
'r' => ($rgb >> 16) & 0xFF,
'g' => ($rgb >> 8) & 0xFF,
'b' => $rgb & 0xFF,
);
foreach($colors AS $colorName => $colorValue) {
//->and split it into pieces at every new line.
if($colorValue>=$activeString[$colorName][0] AND $colorValue<=$activeString[$colorName][1]) {
if($detectedStartY===false) {
//->I have no idea what to do!
//We do: mark the start of the line
$detectedStartY=$y;
}
}else{
//the line color is not found anymore
//see if we already detected a line
if($detectedStartY!==false) {
//yes we did so we write down the area between the lines, the \n's are not visible offcourse
$linesFound[$detectedStartY]=$y;
$detectedStartY=false;
}
}
}
}
//->Please help!
//sure, see the beautiful results:
//because we all love tables
echo '<table width="100%">';
echo '<tr><td valign="top">'; //and we love inline styling, just so fast
echo '<img src="data:image/png;base64, '.$stringSourceBase64.'" border=1 />';
echo '</td><td valign="top">';
//show pieces
$i=0;
foreach($linesFound AS $startY => $endY) {
if($startY==$endY) {
continue;
}
$newHeight=$endY-$startY;
$dest = imagecreatetruecolor($width, $newHeight);
// Copy
imagecopy($dest, $im, 0, 0, 0, $startY, $width, $newHeight);
// Output and free from memory
ob_start();
imagepng($dest);
$contents = ob_get_contents();
ob_end_clean();
echo '
Part #'.$i.' of string <small>(y= '.$startY.' - '.$endY.'px)</small><br>
<img src="data:image/png;base64, '.base64_encode($contents).'" border=1 />
<p>
';
imagedestroy($dest);
$i++;
}
imagedestroy($im);
echo '</td></tr>';
echo '</table>';
//images courtesty of:
//http://indulgy.net/cC/V8/MF/0002501105.jpg
//http://2.bp.blogspot.com/_JGIxXn5d7dc/TBbM2Zu8qRI/AAAAAAAAABE/8WlYvhPusO8/s320/thong4.jpg
//http://cdn.iofferphoto.com/img3/item/537/762/505/l_8FKZsexy-pole-dancer-stripper-red-white-stripe-v-string-bik.jpg
//
//http://stackoverflow.com/questions/2329364/how-to-embed-images-in-a-single-html-php-file
from random import randint
def splitstring(s):
while len(s):
n=randint(2,20)
yield s[:n]
s=s[n:]
astring="This is a string. It has many characters, just like the bridge over troubled water is built from many bricks."
for i in splitstring(astring):
print i
我不想这么刻薄,所以这是一段有效的Python代码,可将您的字符串分成多个部分。但是,由于您没有指定要分割的位置,因此我只选择随机位置。我希望你能接受。
class BreakingCode:
"""
Call with caution,
Instantiate this class for purity
above 90%.
"""
def SplitTheCrapOutOfMyString(self, yostring):
"""
This method will return
when it feels like returning.
"""
print "Hey, how'you doin?" # Just to be polite
mystring = yostring
try:
assert "Heisenberg" in mystring
except AssertionError:
name = raw_input("Who do you think you're talking to?\n>>>")
if name.startswith("H"):
print "Yo, Mr.White"
else:
print "I'm the one who knocks"
for eachword in mystring.split():
print "{:_^40}".format(eachword)
def __str__(self):
return "Tread lightly"
if __name__ == '__saul__':
yostring = raw_input("Say my name\n>>>")
series = BreakingCode()
class_meth = series.SplitTheCrapOutOfMyString(yostring)
input()
对于支持正则表达式并具有 split
易于使用的语言,应始终使用它来分割字符串。这可以帮助您避免重新发明轮子,并使代码简洁明了。使用正则表达式还允许您将代码移植到另一种语言,而无需更改正则表达式。
有一个明显的解决方案,您可以按\n
或进行拆分\r\n
:
爪哇
String result = input.split("\n|\r\n");
的PHP
$result = preg_split('/\n|\r\n/', $input);
该解决方案是垃圾,永远不要使用。在当今时代,避免使用Unicode是徒劳的,而是每个程序员都应该接受它,并确保您的应用程序支持Unicode。如果您仅考虑将其用作\n
或\r\n
用作新的行分隔符,那么您正在编写90年代的软件。在这个Unicode时代,您必须考虑U + 0085,U + 2028,U + 2029是有效的行分隔符。由于Unicode会不时地进行更新,并且通常需要一些时间才能意识到它已被更新,因此可能在Unicode中添加了新的行分隔符。不用,因为所有正则表达式引擎都支持Unicode,并且会定期更新它们以符合最新的Unicode标准。因此,如果您使用的是解释型语言,则无需执行任何操作即可更新您的代码。
要通过行终止符分割字符串并保持最新的Unicode演变,请提供正则表达式^
并指定MULTILINE
模式。
默认情况下,^
仅匹配字符串的开头。在MULTILINE
模式下,^
也匹配行的开始,即行终止符之后。
例如:
爪哇
String result = input.split("(?m)^");
的PHP
$result = preg_split('/^/m', $input);
请注意,前面还有一个额外的空字符串条目,只需将其删除或从索引1循环即可。
乍看起来,这似乎是一个(某种)有效解决方案的好答案,再加上解释和一些编码最佳实践的建议。但是,解决方案本身就是一个巨魔(“我知道,我将使用正则表达式。”现在它们有两个问题。),整个帖子中充斥着一些微妙的错误信息,这将使任何新手都无法参与编程。
MULTILINE
模式,行为^
和$
依赖于“行结束”的定义。Java的考虑\r\n
,\n
,\r
,\u0085
,\u2028
,\u2029
是行终止符,其中\r\n
序列被认为是原子的。JavaScript的考虑\n
,\r
,\u2028
,\u2029
是行终止。Ruby仅认为\n
是行终止符。split
函数对于极端情况可能具有不同语言的不同语义。Python不会在空匹配项上拆分,Java会删除结尾的空字符串(除非您指定负数限制),JavaScript不会在索引为0的空字符串匹配项上拆分。new_string=`echo $string`
这将用换行符分隔字符串。如果回显$new_string
,您会注意到它将新行替换为数组分隔符。
样本输出:
[glitchmr@guava ~]$ string=$'some\nnice\nstring'
[glitchmr@guava ~]$ echo "$string"
some
nice
string
[glitchmr@guava ~]$ new_string=`echo $string`
[glitchmr@guava ~]$ echo "$new_string"
some nice string
[glitchmr@guava ~]$
这不会从文件中读取。使用正则表达式。该代码假定读取的字符串具有'\ n'字符以指示换行符。数字1,2,3,4用于指示拆分。
public static void main(String args[])
{
String strSource = "1.This is a string.This is a string.This is a string.This is a string.This is a string.\n2.This is a string.This is a string.This is a string.This is a string.This is a string.\n3.This is a string.This is a string.This is a string.This is a string.This is a string.\n4.This is a string.This is a string.This is a string.This is a string.This is a string.";
String[] tokens = Pattern.compile("\n").split(strSource,10) ;
for (int loop=0;loop<tokens.length;loop++)
System.out.println(tokens[loop]);
}
static class Module1{
public static void Main()
{
dynamic i = 0;
foreach (object line_loopVariable in split_into_lines(Console.In.ReadToEnd())) {
line = line_loopVariable;
i += 1;
Console.WriteLine("Line {0}: {1}", i, line);
}
}
public static IEnumerable<string> split_into_lines(string text){
dynamic temp_file_name = System.IO.Path.GetTempFileName();
System.IO.File.WriteAllText(temp_file_name, text);
return System.IO.File.ReadLines(temp_file_name);
}
}
您没有指定要在其中分割字符串的“换行”是区分大小写还是不区分大小写。我认为不敏感。
public class SplitStringAtNewline
{
public static final String STRING_TO_SPLIT = "Hellonew lineWorld";
public static void main (String [] args)
{
System.out.println (
String.join("",
Pattern.compile("[nN][eE][wW] [lL][iI][nN][eE]")
.splitAsStream(STRING_TO_SPLIT)
.map((s) -> s + "\n")
.collect(() -> new ArrayList<>(),
(c, e) -> c.add(e), (c1, c2) -> c1.addAll(c2))));
}
}
杜德,这在Powershell中非常容易做到。
像这样获取您的字符串:
$string = "Helloworld!"
然后遍历随机ascii,直到将字符串分成两个这样:
Do {
1..($string.length+1) | % {$new_string+=[char](random (33..127))}
rv new_string
} Until ($new_string -eq ($string.insert(($string.length/2)-1," ")))
最终,您应该获得分割字符串,可以像这样输出:
Write-Host $new_string
输出:
你好,世界!
bash做得很好!
是的,可以通过非常简单的方式来分割字符串:
string=$'foo\nbar\nbaz'
首先,您必须初始化一个变量,该变量将用于存储拆分结果:
declare -a lines
现在,由于每一行都由两个分隔符(字符串的开头或结尾)定界,因此您将需要一个变量来存储第一行
limitA=0
好的,现在您可以搜索分隔符并使用loop存储行。由于bash无法使用二进制值,因此您可以使用类似于od
十六进制值的工具进行采样:
while read hexline
do
addr=${hexline%% *}
hexline="${hexline#$addr}"
addr=$((16#$addr))
for field in $hexline
do
if [ "$field" = "0a" ]
then
lines+=( "${string:limitA:addr-limitA}" )
limitA=$(( addr + 1 ))
fi
((addr++))
done
done < <(od -A x -t x1 <<<"$string")
现在,我们将分割后的字符串存储到变量中lines
:
set | grep ^lines=
lines=([0]="foo" [1]="bar" [2]="baz")
我们可以使用以下命令进行打印:
for (( idx=0 ; idx < ${#lines[@]} ; idx++ ))
do
echo ${lines[idx]}
done
将所有这些放到一个脚本中:
#!/bin/bash
string=$'this is a very long string containing spaces\nshorted, but containing comas...\nthird line.'
declare -a lines
limitA=0
while read hexline
do
addr=${hexline%% *}
hexline="${hexline#$addr}"
addr=$((16#$addr))
for field in $hexline
do
if [ "$field" = "0a" ]
then
lines+=( "${string:limitA:addr-limitA}" )
limitA=$(( addr + 1 ))
fi
((addr++))
done
done < <(od -A x -t x1 <<<"$string")
for (( idx=0 ; idx < ${#lines[@]} ; idx++ ))
do
echo $idx: ${lines[idx]}
done
这将打印:
0: this is a very long string containing spaces
1: shorted, but containing comas...
2: third line.
但是,使用现代的bash实现,您可以将控制字符(如换行符)存储到变量中,甚至进行测试:
#!/bin/bash
string=$'foo\nbar\nbaz'
declare -a lines
limitA=0
for (( idx=0 ; idx < ${#string} ; idx++ ))
do
if [ "${string:idx:1}" = $'\n' ]
then
lines+=( "${string:limitA:idx-limitA}" )
limitA=$(( idx + 1 ))
fi
done
lines+=( "${string:limitA}" )
for (( idx=0 ; idx < ${#lines[@]} ; idx++ ))
do
echo ${lines[idx]}
done
但是,如果您不关心可读性,则可以编写以下压缩脚本:
IFS=$'\n' read -d'' -a lines <<<$'foo\nbar\nbaz'
打高尔夫球的脚本可能显示为:
#!/bin/bash
IFS=$'\n' read -d'' -a lines <<<$'foo\nbar\nbaz'
printf "%s\n" ${lines[@]}
并会产生相同的效果:第一行拆分字符串并将其存储在名为lines的数组中。第二行将打印数组“ lines ”的每个成员,后跟换行符。
但是,随着许多人使用基于ANSI VT标准的文本控制台,您可以使用控制台的VT行为并将其写得更短:
#!/bin/bash
echo $'foo\nbar\nbaz'
将给出相同的结果。