简体   繁体   English

Javascript-替换字符串中的一定数量的字符并保留其余字符

[英]Javascript - replacing a certain amount of characters in a string and leaving the rest

This is a weird question. 这是一个奇怪的问题。

Essentially, I thought it would be a cool idea to let a user type a string, have that string converted to base64, and create an image. 本质上,我认为让用户键入一个字符串,将该字符串转换为base64,然后创建图像是一个不错的主意。

Although I'm not learned in base64, I know images have header data, and need to be a certain length. 尽管我不是在base64上学习的,但我知道图像具有标头数据,并且需要一定的长度。 I've managed to separate the base64 data that tells the colors of the image. 我设法分离出告诉图像颜色的base64数据。 Right now, I have a basic white image - you can check a js fiddle here - (not sure if it's working there, but it works in my browser). 现在,我有一个基本的白色图像- 您可以在此处检查js小提琴- (不确定它是否在那里工作,但可以在我的浏览器中工作)。

Anyway, the variable "imgdata" is the raw data for my PNG image, and that's what I'd like to change. 无论如何,变量“ imgdata”是我的PNG图像的原始数据,这就是我想要更改的内容。 Unfortunately, that string seems to need a certain amount of characters, or the image won't work (guessing it's a size specified in the header?) 不幸的是,该字符串似乎需要一定数量的字符,否则图像将无法正常工作(猜测其大小是否在标题中指定?)

Because it is unlikely that a user inputted string will always reach that same number of characters when converted to base64, I would like to know how to replace the first part of a string, and leave the rest alone. 因为用户输入的字符串在转换为base64时不太可能总是达到相同数目的字符,所以我想知道如何替换字符串的第一部分,而剩下的部分不用管。

TL;DR TL; DR

I have this string - 我有这个字符串-

aaa

and I want to replace this string - 我想替换此字符串-

123456

Since string1 is only 3 characters, I only want it to replace the first 3 characters of string2 so the outcome would look like 由于string1只有3个字符,我只希望它替换string2的前3个字符,所以结果看起来像

aaa456

remember that string1 will vary in length. 请记住,string1的长度会有所不同。

Create a String containing the default values: 创建一个包含默认值的字符串

var fullLength = "abcdefg";

Get the user input as another string: 获取用户输入的另一个字符串:

var user = "aaa";

Create a third string which is a combination of the user data and the end of the default: 创建第三个字符串,该字符串是用户数据和默认结尾的组合:

var combined = user + fullLength.substring(user.length);

Because JavaScript is a weakly typed language, you need to ensure you are working with String objects. 因为JavaScript是一种弱类型语言,所以您需要确保正在使用String对象。 You can do this by concatenating with an empty string which will make sure all of the string functions work: 您可以通过连接一个空字符串来做到这一点,这将确保所有字符串函数都能正常工作:

var userString = userInput + "";

Also, there are three very similar String functions, slice() , substr() , and substring() . 另外,还有三个非常相似的String函数, slice()substr()substring() Do some research on MDN to see which is best for you. 对MDN进行一些研究,看看哪种最适合您。

Javascript slice method can be used alone to solve the problem. 可以单独使用Javascript slice方法解决问题。 The first parameter for slice defines the first character position to grab. slice的第一个参数定义要获取的第一个字符位置。 The second parameter (stop position) can be omitted which means you will get the rest of the string. 可以省略第二个参数(停止位置),这意味着您将获得字符串的其余部分。 By using the length of a as first parameter means it will skip as many character as the length of a and then pad with the rest of the contents of b: 通过将a的长度用作第一个参数,意味着它将跳过与a的长度一样多的字符,然后填充b的其余内容:

a + b.slice(a.length) // 'aaa456'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM